Arrays

An array is a variable containing more than one data. Every element in the array must have a name, called key. Name can be a string or a number. This is the usual syntax to create an array:

```
put "Hello world" into myArr[1]
put "Example" into myArr["test"]

```

You create multidimensional arrays indicating more than one key, look 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 dimension of the array. Beware that when an array add a dimensional key, the content is replaced with the new array 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 empty an element of the array. If you delete an element, you can't recover its key:

```
delete variable myArr["test"]

```

If you **empty** an element, the element remains in the array, but it is empty:

```
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 like:

```
#Max Red, CEO
#Clark Blue, VIP
#Chuck Green, STAR

```

```
put  "Max Red, CEO" & return & "Clark Blue, VIP" & return & "Chuck Green, STAR" into myVar

```

now let's see different methods:

* by one char

```
split myVar by comma

```

it transforms variable in this array:

```
## KEY | VALUE
1   | Max Red
2   | CEO Clark Blue
3   | VIP Chuck Green
4   | STAR

```

* by two chars, the first for the row, the second separate key from value:

```
split myVar by  return and comma

```

it transforms variable in this array

```
## KEY         | VALUE
Max Red     | CEO
Clark Blue  | VIP
Chuck Green | STAR

```

* by row

```
split myVar by row

```

it transforms variable in this array

```
## KEY | VALUE
1   | Max Red, CEO
2   | Clark Blue, VIP
3   | Chuck Green, STAR

```

* by column (for example TAB chars is the standard separator)

```
put "first" & TAB & "Second" into myVar
split myVar by column

```

it transforms variable in this array

```
## KEY | VALUE
1   | first
2   | second

```

Combine command and convert an array in a string, with the same mechanism of SPLIT.

Arrays can't be visualized, only string can be visualized on the screen. The debbuger shows the array in a tree style, in the variables section when the program is stopped for errors or debbuging.

You can save arrays on a file using arrayDecode and arrayEncode, these functions coverts array in 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