Arrays

Creating Arrays

As with frames, you create arrays just by using the array square brackets, [ ]. For instance, this creates a new empty array:

new := [];
Accessing array elements is similar to most languages:

y := x[0];
z := x[1];
zz := x[2];
As you might have guessed, NewtonScript array indexes start at 0 (the first element is at index 0). To find the length of an array, you can use the built-in Length function. Here is an example that prints out the elements of an array:

x := [15, "xyz", {a: 2, b: 3}];
for i := 0 to Length(x) - 1 do
   Print(x[i]);
Though this code works, there is a much better way to iterate through the elements of an array (see "Iterating with foreach" on page 55).


An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.

Last modified: 1 DEC 1996