DIM var(dim) , [var(dim)]…
DIM var$(dim) LENGTH n
Specifies a variable that is an array with one or more dimensions. The variables can be numbers or strings with multiple declarations separated by commas.
‘dim’ is a bracketed list of numbers separated by commas. Each number specifies the number of elements in each dimension. Normally the numbering of each dimension starts at 0 but the OPTION BASE command can be used to change this to 1.
For example: DIM nbr(10, 20) specifies a two dimensional array with 11 elements (0 to 10) in the first dimension and 21 (0 to 20) in the second dimension. The total number of elements is 231 and because each number requires 4 bytes a total of 924 bytes of memory will be allocated.
String arrays will by default be allocated memory for 255 characters for each element and this can quickly use up memory. In that case the LENGTH keyword can be used to specify the amount of memory to be allocated to each element. This allocation (‘n’) can be from 1 to 255 characters.
For example: DIM str$(5, 10) will declare a string array with 66 elements consuming 16,896 bytes of memory while: DIM str$(5, 10) LENGTH 20 Will only consume 1,386 bytes of memory. Note that the amount of memory allocated for each element is n + 1 as the extra byte is used to track the actual length of the string stored in each element.
Examples:
DIM nbr(50)
DIM str$(20)
DIM a(5,5,5), b(1000)
DIM str$(200) LENGTH 20
1 2 3 4 5 6 7 8 |
10 Data 1, 2, 3, 4, 5, 6, 7, 8, 9 20 Dim var(9) 30 For loop = 1 To 9 40 Read var(loop) 50 Next loop 60 For loop = 1 To 9 70 Print var(loop) 80 Next loop |
> run
1
2
3
4
5
6
7
8
9
If a string longer than ‘n’ is assigned to an element of the array an error will be produced. Other than this string arrays created with the LENGTH keyword act exactly the same as other string arrays.
See also:
CLEAR
DATA
ERASE
LET
OPTION BASE
READ
RESTORE