Livecode Wiki
Advertisement

An array is composed of a group of elements all having the same name, with each element of an array having a different set of keys.

Elements[]

For every element of an array, a set of keys are associated with a value. In LiveCode elements are created one at a time and do not have to be consecutive.

The general syntax to create an element is:

put Value into Array[Keys] 

Keys[]

A key can be a string or a number. If a key is a number, it may also be referred to as an index. The keys differentiate one element of an array from another. Any two elements of an array have a different set of keys.

  • myArray[1] = myArray["1"] = myArray[X] (if X is a variable with value of 1)
  • myArray[temp] = myArray["temp"] (if temp is not assigned as a variable).
  • If you want to use a numerically indexed array, then only use numeric keys for your array.
  • If you want to use a non-numeric keyed array, then only use non-numeric keys for your array.

Values[]

Every element of an array contains a value - an integer, floating point, string, or boolean. Each element of an array may contain a different value or even a different type of value. An element of an array can be used anywhere a variable can be used.

For example:

put "Hello world" into myArr[1]         -- myArr[1] = "Hello world"   String
put 123 into myArr["test"]              -- myArr["test"] = 123        Integer
put true into myArr[5]                  -- myArr[5] = true            Boolean

Dimensions[]

The dimension of an array is determined by the number of keys in each element of the array. If there is only one key for each element, then it is a one-dimensional array, also called a vector or a linear array. If there are two keys for each element, then it is a two-dimensional array and so forth. An array can have any number of dimensions, just make sure that all of its elements have the same number of dimensions.

For example:

put "Parakeet"   into  miniDart["Birds"]           -- one dimension or linear array
put "Mouse"      into  Bitart[4,5]                 -- two dimension array
put "Lizard"     into  yourGat[3,reptile,8]        -- three dimension array
put "Cockroach"  into  hisNal[Insects, 9 45, 61]   -- four dimension array

A two-dimensional array can be thought of as a linear array of linear arrays. In other words, you can create a two-dimensional array by using a set of two keys. For example, a two-dimensional array:

 put "Bats"    into  maxDim[1,1]
 put "Pidgins" into  maxDim[1,2]
 put "Doves"   into  maxDim[2,1]
 put "Eagles"  into  maxDim[2,2]

Deleting[]

You can delete or or get rid of one element of the array by using the delete command. Once deleted, you can't recover its value or key.

delete variable myCircuit["test"]  -- the element myCircuit["test"] no longer exists

If you put empty into an element, the value is deleted, but the element remains in the array. For example:

put empty into myWire["test"]      -- the element myWire["test"] = empty

You can delete an entire array by putting empty into an array. Both keys and values will be deleted:

put empty into myMax               -- the array myMax no longer exists

You can obtain the list of the keys from an array with the keys keyword:

put the keys of myArray into allMyKeys

This only works for a one-dimensionals array, it doesn't recover any subkeys of higher dimension arrays. Look over 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

Lists and Arrays[]

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 & "Ed Blue, VIP" & return & "Bill Green, STAR" into myRat
                             /* myRat = Max Red, CEO
                                        Ed Blue, VIP
                                        Bill Green, STAR */
split myRat by comma 
/* myRat[1] = Max Red
   myRat[2] = CEO
              Ed Blue
   myRat[3] = VIP
              Bill Green
   myRat[4] = STAR */
split myRat by  return and comma
/* myRat["Max Red"] = CEO
   myRat["Ed Blue"] = VIP
   myRat["Bill Green"] = STAR */
split myRat by row
/* myRat[1] = Max Red, CEO
   myRat[2] = Ed Blue, VIP
   myRat[3] = Bill 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[1] = First day
   myCat[2} = Second hour
   myCat[3] = Third minuet */

The combine command will convert an array into a list, provided that the keys are numbers. The first, third and fourth examples above could be converted back into a list variable with the Combine command.

Associative Arrays[]

In LiveCode, all arrays are associative arrays. That means that the keys of the element are associated to the value. The internal representation of an array in LiveCode is as a vector or linear array. Every different set of keys creates a different element of the array.

Showing Arrays[]

Arrays can't be visualized by LiveCode in a field or answer dialog. However, the debugger shows the array when the program stops for errors or breakpoints. The array is shown at the bottom of the Script Editor in the Variable Section in a tree style.

Arrays and Files[]

You can save an array into a file using arrayEncode. This function coverts arrays to a binary format that LiveCode can export. The arrayDecode 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

Advertisement