Syntax
Declares an array, with one or more dimensions, whose bounds are inclusive. UNDIM empties it.
DIM <name>(<size>[, <size>…]) [AS <type>] [= [<value>, …]]
DIM <name>[<size>[, <size>…]] [AS <type>] [= [<value>, …]]
UNDIM <name>[]Also written : UNDIM
Parts
| <name> | The array's name. A name ending in $ starts every cell at "" instead of 0. |
|---|---|
| <size> | The last valid index of one dimension, not the number of cells. Any expression: DIM grid(width, height). |
| <type> | Optional. A number or string type (integer, uint8, float…) converts every write; a record type makes every cell a record. |
| <value> | Optional list of starting values, written in square brackets, stored from index 0. One-dimensional arrays only; a longer list grows the array. |
Good to know
DIM a(10)has 11 cells, indexed 0 to 10, as in AGK, DarkBASIC and PureBasic.a.lengthgives the last index, 10.a(i, j),a[i, j]anda[i][j]reach the same cell.- There is no bounds check. Reading past the last index gives no value (
Strshowsundefined); writing past it silently grows the array. DIMon a name that already exists makes a new array and loses the old values. To resize and keep them, assign.length.- A
DIMat the top of the program is seen by every FUNCTION, even in a program with aDO … LOOP. ADIMinside a FUNCTION belongs to that call. UNDIM a()with round brackets compiles into a program that does not start. WriteUNDIM a[].GLOBAL DIM g(2)is broken: the next use ofg(…)is refused as an unknown command. Write a plainDIMat the top of the program.DIM x AS integer, with no size, compiles but the program does not start. A single typed value is declared withx AS integer.
Example
DIM scores(4)scores(0) = 10scores(4) = 50Console(Str(scores.length) + " " + Str(scores(0) + scores(4))) ' 4 60DIM grid(2, 3) AS integergrid(1, 2) = 7.9Console(Str(grid[1][2])) ' 7UNDIM scores[]Console(Str(scores.length)) ' -1In the manual : Arrays, lists and maps