The NEWARRAY command creates one array, specifying its name, the number of dimensions and the size of each dimension. An array is an ordered collection of vectors that are referenced by a name and a set of one or more index numbers separated by enclosing square brackets. Each set of square brackets represents one dimension of the array. The NEWARRAY command may be used inside or outside a subroutine.
The NEWARRAY command's first argument is a name which follows the same rules as any vector name. The array name must not be the same as that of a non-array variable. Following the array name are one or more numbers, which may be variable names, specifying the size of each dimension of the array. For example, the next command creates an array named "myArray" with two dimensions:
NEWARRAY myArray 10 20
This command creates an array with 10 rows and 20 columns. That allows room for 10x20 = 200 vectors in the array. Once an array is declared, its dimensions cannot be changed.
You refer to locations in the array by using its name followed by index numbers enclosed within square brackets. There must always be the same number of indexes as are listed in the NEWARRAY command. Here is an example array variable specifying a location in the array myArray that was declared in the above NEWARRAY command:
This specifies the vector located at row 3, column 5 of myArray. Such array variables can be used anywhere that a vector variable may be used. So this is legal:
LET myArray = 5 * SIN(myArray)
Further, it is even legal, though unusual, to use an array variable as an index in another array variable, like this:
The indexes may be variable. So, for example you might have loops like those shown in the example at right that scan an entire array using the variables row and col to select the specific array entry.
The entire array is either global or not global. You cannot declare an individual array entry (such as myArray) to be global.
There is currently no way to pass an entire array to a subroutine as an argument; only individual array locations (vectors) can be passed. A workaround for this is to declare the array GLOBAL. Then it can be created outside the subroutine and used inside the subroutine.
The following example program creates an array, fills all its locations with vectors containing consecutive numbers, then prints out the entire array as a table:
NEWARRAY myArray 10 20 counter = 1 'Fill array with consecutive numbers FOREACH row 1,10 FOREACH col 1,20 myArray[row][col] = counter counter = counter + 1 END END 'Print out the array contents as a table: PRINT "Row\tCol\tmyArray[row][col]" 'Print header first FOREACH row 1,10 FOREACH col 1,20 OUTPUT "%5.3F\t%5.3F\t%5.3F\n" row col myArray[row][col] END END