FANDOM


An array is a variable containing more than one value. Each array has a name. The values in an array are called the elements of the array. Each element has a key. The key is a string or a number. To access the values in an array, the name of the array and the key must be given. You can use an element of an array any place you would use a variable.

For example, to create an element of an array:

put "Hello world" into myArr[1]                  -- myArr[1] = "Hello world"
put "Example" into myArr["test"]                 -- myArr["test"] = "Example"
put myArr["test"] && myArr[1] into myArr[4]      -- myArr[4] = Example Hello world"

You can create multidimensional arrays indicating more than one key, look at the following example:

 put "Position 1,1" into myArr[1,1]
 put "Position 1,2" into myArr[1,2]
 put "Position 2,1" into myArr[2,1]
 put "Position 2,2" into myArr[2,2]

There is no limit to the number of dimensions for the array. 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[1]
put "Bye" into myArr[1,2]      -- here the "Hello" doesn't exist anymore

In the previous example myArr[1] becomes the content of the array [2], this is useful in recursive operation on arrays, because you may pass the just 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 myArr["test"]      -- myArr["test"] no longer exists

If you empty an element, the element remains in the array, but has no value.

put empty into myArr["test"]

You can delete an entire array with empty, both keys and values will be deleted:

put empty into myArr

In order to empty a complex array, use 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 listOfElements

Note that keys obtain just a single dimension of keys, it doesn't recover all subkeys, look at the following example:

#let's create a matrix 2x3
 put "Position 1,1" into myArr[1][1]
 put "Position 1,2" into myArr[1][2]
 put "Position 1,3" into myArr[1][3]
 put "Position 2,1" into myArr[2][1]
 put "Position 2,2" into myArr[2][2]
 put "Position 2,3" into myArr[2][3]
 put the keys of myArr  #this obtain just 1 and 2 
 put the keys of myArr[1]  #this obtain 1, 2 and 3
 put the keys of myArr[2]  #this obtain 1, 2 and 3

You can transform a variable containing a string in an array. 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 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 myVar                -- myVar = Max Red, CEO
                     --         Clark Blue, VIP
                     --         Chuck Green, STAR 

split myVar by comma 
-- myVar[1] = Max Red
-- myVar[2] = CEO Clark Blue
-- myVar[3] = VIP Chuck Green
-- myVar[4] = STAR
split myVar by  return and comma
-- myVar["Max Red"] = CEO
-- myVar["Clark Blue"] = VIP
-- myVAR["Chuck Green"] = STAR

split myVar by row
-- myVar[1] = Max Red, CEO
-- myVar[2] = Clark Blue, VIP
-- myVar[3] = Chuck Green, STAR

Sometimes values in a list are separated by TAB chars.

put "First day" & TAB & "Second hour" & TAB & "Third minuet" into myVar 
split myVar by column 
-- myVar[1] = First day
-- myVar[2} = Second hour
-- myVar[3] = Third minuet

The Combine command and convert an array in a string, 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, only string can be visualized on the screen. The debugger shows the array in a tree style, in the variables section when the program is stopped for errors.

You can save arrays on a file using arrayDecode and arrayEncode, these functions coverts array to a binary format that Livecode can export and import directly. 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

Community content is available under CC-BY-SA unless otherwise noted.