(available in almost any device except for very old ones - see Feature Matrix)
In some of MKT's programmable terminals, the built-in interpreter supports a numeric array. The array function has been implemented in Summer 2004 as a storage memory for a special extension to the terminal's diagram function (polygon overlays), but it can be used for other purposes also. More powerful arrays are supported by the script language, but the array functions in the display interpreter are still available for compatibility. Last not least, the old global array described in this document can also be accessed via script (display.arr[row][column][component]) to ease migration from 'display events' to the script language.
Some properties of the numeric array are:
To find out if your terminal supports numeric arrays, look into the Feature Matrix. Details about arrays in the script language are here.
The values in the global array (named "arr
") can be
accessed like normal floating-point variables. The array indices must be
appended after the array's name in squared brackets.
The terminal's numeric array is three-dimensional by default (may be changed with the "dim"-command one day). For simplicity, let's assume our three dimensional array is arranged in rows, columns, and components (=single values) as in the following 10*5*4-array (10 rows, 5 columns per row, 4 components in each column). The four components in each column may be considered the coordinates of a point. Here, it doesn't matter what the indexes are used for and how you call them in your application.
For the examples below, let's assume there are already some values stored in the array:
column #0 | column #1 | column #2 | column #3 | column #4 | |
row #0 |
0, 1, 2, 3 |
4, 5, 6, 7 |
8, 9, 10,11 |
12,13,14,15 |
16,17,18,19 |
row #1 |
20,21,22,23 |
24,25,26,27 |
28,29,30,31 |
32,33,34,35 |
36,37,38,39 |
row #2 |
40,41,42,43 |
44,45... |
... |
... |
... , 49 |
row #3 |
60,... |
... |
... |
... |
... , 59 |
row #4 |
80,... |
... |
... |
... |
... , 69 |
row #5 |
100,... |
... |
... |
... |
... , 79 |
row #6 |
120,... |
... |
... |
... |
... , 89 |
row #7 |
140,141,142,143 |
144,145,146,147 |
148,149,150,151 |
152,153,154,155 |
156,157,158,159 |
row #8 |
160,161,162,163 |
164,165,166,167 |
168,169,170,171 |
172,173,174,175 |
176,177,178,179 |
row #9 |
180,181,182,183 |
184,185,186,187 |
188,189,190,191 |
192,193,194,195 |
196,197,198,199 |
Please note that like in the "C" programming language, array
indices run from ZERO to <max size minus one>. For the 10*5*4-table
shown above, the row index runs from 0 to 10, the colums from 0 to 4, and the
components from 0 to 3.
The basic syntax to access an array is:
arr[<column>][<row>][<component>]
arr[0][0][0]
arr[0][1][2] := 6.123
To save space in the program memory, you can set multiple values in one command by referencing a whole range of indexes in a single command. For example, you may want to clear everything in row 0 (all columns in that row, and all components of the columns), use a command like this:
arr[0][0..4][0..3] := 0.0
Accessing a complete row may be abbreviated by leaving the column- and component-index-range away like this:
arr[0] := 0
arr[7][0] := 0
For special applications, you may need the index when accessing values in the array. For this purpose, up to three automatic variables (one for every dimension) can be used. The names of such automatic variables consist of a lower-case letter like in these examples:
arr[i=0..9][j=0..4][k=0..3] := 20*i + 4*j + k
i,j,k
). The
whole term is calculated 200 times in the internal loops, which is the reason
why the interpreter needs a bit longer for this (compared to a
"simpe" assignment).Note: The "to"-operator (..) can be abbreviated with a tilde (~), so
arr[i=0
~9][j=0
~4][k=0
~3] := 20*i + 4*j + k
arr[i=0..9][j=0..4][k=0..3] := 20*i + 4*j + k
Here are some more examples for filling a range of cells in an array:
arr[0][i=0..3][j=0..1] := sdo(0x2941.(3+j+2*i),FLT) : rem read 4 pts
arr[0][0][0] := sdo(0x2941.3,FLT) : rem read column 0, row 0,
X-coord
arr[0][0][1] := sdo(0x2941.4,FLT) : rem read column 0, row 0, Y-coord
arr[0][1][0] := sdo(0x2941.5,FLT) : rem read column 0, row 1, X-coord
arr[0][1][1] := sdo(0x2941.6,FLT) : rem read column 0, row 1, Y-coord
arr[0][2][0] := sdo(0x2941.7,FLT) : rem read column 0, row 2, X-coord
arr[0][2][1] := sdo(0x2941.8,FLT) : rem read column 0, row 2, Y-coord
arr[0][3][0] := sdo(0x2941.9,FLT) : rem read column 0, row 3, X-coord
arr[0][3][1] := sdo(0x2941.A,FLT) : rem read column 0, row 3,
Y-coord
Finally, you can initialize a number of rows and columns in a single command, assigning individual values to the cells:
arr[0][0..3][0..1] := {{-20,0},{0,30},{20,0},{0,-30}}
arr[0][0][0] := -20
arr[0][0][1] := 0
arr[0][1][0] := 0
arr[0][1][1] := 30
arr[0][2][0] := 20
arr[0][2][1] := 0
arr[0][3][0] := 0
arr[0][3][1] := -30
Arrays can be used as parameters, when a lot of "values" must be passed to a subroutine (function call). Some of the built-in functions expect a number of points (with X- and Y-coordinate) passed in the argument list, for example a command to draw polygons as "overlay" into a diagram. An example:
dia.poly(arr[0~8][0~3])
In natural language, this command would say, "hello diagram, draw a polygon, taking the coordinates from an array named "arr", but only look at rows 0 to 8, and for each of these rows draw a polygon using rows 0 to 3". So there will be nine polygons with four corners each. What you don't see in this call is where the X- and Y-component are taken from - in fact "X" is taken from component index 0 and "Y" from component index 1 (if they are not specified). To make it clear where a point's X and Y - coordinate are taken from, put it this way:
dia.poly(arr[0~8][0~3][0~1])
In this complete array reference it is easier to see that nine rows, four columns per row, and two components per column are passed to the subroutine.
The total number of values in an array cannot exceed 4000, due to an
unfortunate limitation of the CPU (size of an "object" limited to
16384 byte, while a single "float" value occupies 4 byte). So the
product of <row_count> * <columns_per_row> *
<components_per_column> can never exceed 4000.
To change an array's dimensions during run-time, use the interpreter's "dim" command, before entering any values into the array.
dim <arrayname>[number of rows][columns per row][components
per column]
dim arr[10][40][3]
There is a complete example for the array and the "dim"-command in the programs directory in the terminal application arraytest1.cvt . In that example an array with 40 columns per row is defined, which will filled with 40 corner coordinates for a polygon. The polygon will then be drawn into an X/Y-diagramm.
See main manual.