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.length gives the last index, 10.
  • a(i, j), a[i, j] and a[i][j] reach the same cell.
  • There is no bounds check. Reading past the last index gives no value (Str shows undefined); writing past it silently grows the array.
  • DIM on a name that already exists makes a new array and loses the old values. To resize and keep them, assign .length.
  • A DIM at the top of the program is seen by every FUNCTION, even in a program with a DO … LOOP. A DIM inside a FUNCTION belongs to that call.
  • UNDIM a() with round brackets compiles into a program that does not start. Write UNDIM a[].
  • GLOBAL DIM g(2) is broken: the next use of g(…) is refused as an unknown command. Write a plain DIM at 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 with x 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))   ' -1
In the manual : Arrays, lists and maps