An array is a group of variables all having the same name, but different indexes or keys. Index and Key are used interchangeably, but usually an index is considered to be a number and a key is considered to be a string. An element of an array is the name of an array with a particular index. You can use an element of an array any place you would use a variable. Arrays in LiveCode are associative. An element of an array is created when an element of an array is associated with a value.
For example, to create an element of an array:
put "Hello world" into myArr -- myArr = "Hello world" put "Example" into myArr["test"] -- myArr["test"] = "Example" put myArr["test"] && myArr into myNum -- myNum = "Example Hello world"
Arrays can be numerically indexed. For example myArr above. But notice that myArr["test"] does not have a numeric index. Also, notice that myArr = myArr["1"] = myArr[X] (if X is a variable with value of 1). And also myArr[temp] = myArr["temp"] (if temp is not an assigned variable).
If you want to use a numerically indexed array, then only use numeric indexes for your array. If you want to use a non-numeric keyed array, then only use non-numeric keys for your array.
You can create multidimensional arrays indicating more than one index, look at the following example:
put "Bats" into maxDim[1,1] put "Pidgins" into maxDim[1,2] put "Doves" into maxDim[2,1] put "Eagles" into maxDim[2,2]
A single dimensional array is known as a linear array. A two-dimensional array can be thought of as a linear array of linear arrays. In other words, the following is equivalent to that above.
put "Bats" into maxDim put "Pidgins" into maxDim put "Doves" into maxDim put "Eagles" into maxDim
An array can have any number of dimensions. However, when you create some elements of an array, then create some additional elements of that same array with a higher dimension, the previous elements are replaced with the elements of the higher dimension. For example:
put "Hello" into myArr put "Bye" into myArr[1,2] -- myArr doesn't exist anymore
In the previous example myArr becomes the content of the array , this is useful in recursive operation on arrays, because you may pass just the second (or more) level of an array as a new array.
You can delete or or get rid of an element of the array. Once deleted, you can't recover its value or key.
delete variable myCircuit["test"] -- the element myCircuit["test"] no longer exists
If you empty an element, the element remains in the array, but has no value.
put empty into myWire["test"] -- the element myWire["test"] = empty
You can delete an entire array with empty, both keys and values will be deleted:
put empty into myMax -- the array myMax no longer exists
In order to empty a complex array, use a function like this:
function emptyArray tArr repeat for each key tKey in tArr put empty into tArr2[tKey] put emptyArray(tArr[tkey]) into tArr2[tkey] end repeat return tArr2 end emptyArray
You can obtain the list of the keys with the keys keyword:
put the keys of myArray into allMyKeys
Note that the above will only work for a single dimension of keys, it doesn't recover all subkeys. Look over the following example:
#let's create a matrix 2x3 put "Position 1,1" into myArr put "Position 1,2" into myArr put "Position 1,3" into myArr put "Position 2,1" into myArr put "Position 2,2" into myArr put "Position 2,3" into myArr put the keys of myArr #this obtain just 1 and 2 put the keys of myArr #this obtain 1, 2 and 3 put the keys of myArr #this obtain 1, 2 and 3
Suppose you have a variable containing a string or a list. You can transform that variable into an array of the same name. Just use the split command. The split command needs to know how you want to transform the string, so you have to declare how to read the data. Look over the following examples:
First let's create a big string and then look at different versions of the split command:
put "Max Red, CEO" & return & "Clark Blue, VIP" & return & "Chuck Green, STAR" into myRat /* myRat = Max Red, CEO Clark Blue, VIP Chuck Green, STAR */
split myRat by comma /* myRat = Max Red myRat = CEO Clark Blue myRat = VIP Chuck Green myRat = STAR */
split myRat by return and comma /* myRat["Max Red"] = CEO myRat["Clark Blue"] = VIP myRat["Chuck Green"] = STAR */
split myVar by row /* myRat = Max Red, CEO myRat = Clark Blue, VIP myRat = Chuck Green, STAR */
Sometimes values in a list are separated by TAB chars.
put "First day" & TAB & "Second hour" & TAB & "Third minuet" into myCat
split myCat by column /* myCat = First day myCat[2} = Second hour myCat = Third minuet */
The Combine command does the opposite. It will convert an array into a list, provided that the keys are numbers. Any of the 4 above examples could be converted back into a list variable with the Combine command.
Arrays can't be visualized by LiveCode on the screen, only a list can be visualized. The debugger shows the array in a tree style, this is in the variables section when the program is stopped for errors.
You can save an array in a file using arrayDecode. This function coverts array to a binary format that LiveCode can export. The arrayEncode does the opposite. It will convert a binary format into an array so that LiveCode can import. See http://lessons.runrev.com/s/lessons/m/4071/l/9597-how-do-i-store-an-array-in-a-text-file-and-then-retrieve-it-again