Fandom

Livecode Wiki

Arrays

1,965pages on
this wiki
Add New Page
Comments2 Share

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.

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

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

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.