Scripting 5.6. Arrays and dictionaries

You can use arrays of numeric and string variables:

for i from 1 to 5
    square [i] = i * i
    text$ [i] = mid$ ("hello", i)

After this, the variables square[1], square[2], square[3], square[4], square[5], text$[1], text$[2], text$[3], text$[4], and text$[5] contain the values 1, 4, 9, 16, 25, "h", "e", "l", "l", and "o", respectively:

writeInfoLine: "Some squares:"
for i from 1 to 5
    appendInfoLine: "The square of ", i, " is ", square [i]

In the examples above, the index into the array was always a number. A hash or dictionary is an array variable where the index is a string:

age ["John"] = 36
age ["Babs"] = 39
writeInfoLine: "John is ", age ["John"], " years old."

See also

You can use any number of array and dictionary variables in a script, but for many applications, namely whenever it were useful to look at a numeric array as a single object, it may be better to use vectors and matrices (see Scripting 5.7. Vectors and matrices) or to use Matrix or Sound objects.

Links to this page

© ppgb, July 18, 2017