## FANDOM

2,307 Pages

An array is a group of variables, all having the same name. You can use an element of an array any place you would use a variable. Each element of this array has a key. The key is a string or a number. To access any particular element of the array, the name of the array and its key must be given.

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"
```

Please note that arrays are not numerically indexed, so myArr["1"] = myArr[1]. And also myArr[temp] = myArr["temp"], if temp is still not an assigned variable.

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

``` put "Position 1,1" into maxDim[1,1]
put "Position 1,2" into maxDim[1,2]
put "Position 2,1" into maxDim[2,1]
put "Position 2,2" into maxDim[2,2]
```

A single dimensional array is known as a linear array. A two-dimensional can be thought of as a linear array of linear arrays. In other words, the following is equivalent to that above.

``` put "Position 1,1" into maxDim[1][1]
put "Position 1,2" into maxDim[1][2]
put "Position 2,1" into maxDim[2][1]
put "Position 2,2" into maxDim[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 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 = empty
```

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 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[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
```

Suppose you have a variable containing a string. 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[1] = Max Red
myRat[2] = CEO Clark Blue
myRat[3] = VIP Chuck Green
myRat[4] = STAR */
```
```split myRat by  return and comma
/* myRat["Max Red"] = CEO
myRat["Clark Blue"] = VIP
myRat["Chuck Green"] = STAR */
```
```split myVar by row
/* myRat[1] = Max Red, CEO
myRat[2] = Clark Blue, VIP
myRat[3] = 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[1] = First day
myCat[2} = Second hour
myCat[3] = 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