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)
endfor

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]
endfor
=>
Some squares:
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25

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.”
=>
John is 36 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


© Paul Boersma 2017-07-18,2025