Hi,
WebiWan wrote: ↑Tue Dec 31, 2019 8:57 pm
Are there less buggy versions available?
For such I'd recommend LC 6.7.10/11. It is, in my opinion, the last version actually deserving the attribute "Stable". For sure, it lacks a lot of the bells & whistles of the flashier new versions (7, 8, 9), and even a few useful new features. But it's sufficiently bug-free, and it's fast & reliable. Which is a chronic deficiency in the new versions, IMHO.
To your question:
I assume it's a misinterpretation of "elements" - elements are the values of the array, not the keys! So if you plan to use it in square brackets later, "elements" are a no-no! I wrote some code to clarify this. Guess I'll explain it at first.
- I populate the Array. It will result in this:
- the Array
- array.png (7.37 KiB) Viewed 8307 times
- Then I have the previous attempts/ examples.
- WebiWan 1: Your X is the whole sub-array of A, not an (expected) key. Cannot work.
- WebiWan 2: Your X is the ordinal number of a first level key, not a key itself. Will not work, or give wrong results.
- mrcoollion 1: Correctly retrieves the first key (A), but then assumes the second level keys are numeric. They are here, but you cannot rely on it.
- mrcoollion 2: Basically the same.
- Last, a way to correctly loop through the values using the keys. I added a few lines to output a table-like representation of the data in the clipboard. It's always helpful for a quick reference ;-)
Make a new stack with 1 button, and put this into the btn script. "Apply", and hit the btn. Step through & watch the variables - you'll see why the work or not. Hit "Continue" (F5) to skip to the next example:
Code: Select all
on mouseUp
repeat for each char C in "ABCDE" -- 1st level keys
repeat for each char N in "12345" -- 2nd level keys
put "Val_" & C & N into myArray[C][N] -- values
end repeat
end repeat
breakpoint
-- WebiWan 1:
repeat for each element X in myArray
put myArray[X][1] into myVar -- fails because X itself is an array
end repeat
breakpoint
-- WebiWan 2:
repeat with X = 1 to (the number of elements in myArray)
put myArray[X][1] into myVar -- fails because X is not a Key (ABC ... are!)
end repeat
breakpoint
-- mrcoollion 1:
repeat for each key T in myArray
put myArray[T] into myLine
put myLine[1] into myvar -- this works here, because 1 is actually a 2nd Level key (123 ...)
end repeat
breakpoint
-- mrcoollion 2:
repeat for each key T in myArray
put myArray[T][1] into myVar -- this works here, because 1 is actually a 2nd Level key (123 ...)
end repeat
breakpoint
-- Try this!
repeat for each key k1 in myArray -- loop through all 1st level keys
repeat for each key k2 in myArray[k1] -- loop through all 2nd level keys
put myArray[K1][k2] into myVar -- and hit the values
put k1 & tab & k2 & tab & myArray[K1][k2] & CR after myTable -- make table :)
end repeat
end repeat
delete char -1 of myTable
set the clipboarddata["text"] to myTable
end mouseUp
Arrays in LC are basically
simple key-value pairs (1-dimensional).
They are
not table-like structures (2-dimensional) like in databases!
(LC still has no data type for handling database data, nor a good way to display it. There's the "Table field" that never got finished, and there's the "DataGrid" whose main virtue is repelling newbies by it's sheer weight & complexity.)
To work with database data in arrays, you need to blow 'em up to the second dimension - means, each key in the first dimension (the record designator: ID, for instance), has as value a sub-array (the record data).
And each value in this record data sub-array has its own key again - the 2nd level key, or the field name.
You got the basic array right, as it seems, but got messed up addressing it. It takes some time to get used to it ;-)
Meanwhile you may use a function I wrote to make my days more enjoyable - it allows you to create arrays where the keys have meaningful names.
Assume you have a table with fields of "PersonID", PersonName", PersonHeight". You got the data via revdb_something in tab-return format and want it in a nice array. (I added code to provide some sample data)
Code: Select all
on mouseUp
-- load some data:
put "100" & tab & "Joe" & tab & "80" & CR & \
"101" & tab & "Jane" & tab & "60" & CR & \
"102" & tab & "Jimmy" & tab & "30" into myData
-- load the field names
put "PersonID" & tab & "PersonName" & tab & "PersonWeight" into myKeys
-- make the array:
put Data2Array(myData, myKeys, 1) into myArray -- full version
-- put Data2Array(myData) into myArray -- all autonumbered keys
-- put Data2Array(myData, myKeys) into myArray -- records autonumbered, but field names
breakpoint
end mouseUp
function Data2Array theData, theKeys, theKeyCol
/*
Use this to translate tabular data to an 2D array - myArr[x][y]
The keys of the array can be just numbered (autoinc), or can be named according
the contents of one of the columns (for 1st level) resp. according a list of field names (2nd level)
"theData" (tab-del TEXT, ex. data from a db): the data to translate into a 2D array.
"theKeys" (optional, tab-del TEXT): the names of the 2nd level keys. Else autoInc INT.
"theKeyCol" (optional, INT): designates the # of the column that will become the 1st level key. Else autoInc INT.
(Hint: this column MUST NOT contain dupes, so best only use for an autoinc unique id!)
Returns the desired array. Has no errors :)
© axwald 2019, GPL V3
*/
set itemdel to tab
put 0 into MyLCnt
repeat for each line L in theData
add 1 to myLCnt
put 0 into myICnt
repeat for each item I in L
add 1 to myICnt
switch
case (theKeys is empty) and (theKeyCol is empty) -- myArr[1][1]
put I into myArr[myLCnt][myICnt]
break
case (theKeys is not empty) and (theKeyCol is empty) -- myArr[1][aColName]
put I into myArr[myLCnt][(item myICnt of theKeys)]
break
case (theKeys is empty) and (theKeyCol is not empty) -- myArr[anID][1]
put I into myArr[(item theKeyCol of L)][myICnt]
break
case (theKeys is not empty) and (theKeyCol is not empty) -- myArr[anID][aColName]
put I into myArr[(item theKeyCol of L)][(item myICnt of theKeys)]
break
end switch
end repeat
end repeat
return myArr
end Data2Array
Have fun, hope I could help! And a happy new year to all!