DIM DIMension
Statement
DIM is used to dimension (set up) an array of a given number of numeric or string variables. An array is a list of variables of the same name that are distinguished by a subscripts (values that identify each variable or element in the array).
How to use DIM with numeric arrays
DIM is used to form a statement in a program. It is followed by a single letter that names the array, and one or more numeric values each separated by a comma and enclosed in brackets, for example
10 DIM x(10)
20 DIM z(20,5)
In the first case, a one-dimensional array is created containing ten elements with subscripts from 1 to 10. The array has the name x and the subscripted variables are x(1) to x(10) inclusive. Any existing array of the same name is deleted, and the variables are each assigned a value of 0. Note that in dimensioning an array, the BASIC does not distinguish between names with capital and lowercase letters - variable x(2) is the same as X(2). However, simple numeric variables having the same letter as an array name (x or X) can coexist and may be used separately if required.
The number of values in brackets equals the number of dimensions in a numeric array. The second example sets up a two-dimensional array of 100 elements with 20 elements in the first dimension and 5 in the second. These elements are numbered z(1,1) to to z(20,5).
Arrays of any number of dimensions may be created.
The elements of a numeric array may subsequently be identified by the array value in brackets, for example
70 PRINT x(a)
160 PRINT z(7,b)
DIM and string arrays
DIM is used in the same way as with numeric arrays except that a single letter followed by $ is used for the array name. Furthermore, an extra value must be added to the dimension values in brackets in order to define the length of each string. For example
30 DIM a$(20,5)
90 DIM b$(20,5,10)
The first statement creates an array of 20 elements, each of which contains a string of 5 characters. The subscripted variables are named a$(1) to a$(20) inclusive, and they are initially assigned a null (empty) string (""). Any existing array of the same name is deleted and, unlike numeric arrays, a simple string variable of the same name cannot coexist.
The second example creates a two-dimensional array of 100 elements with 20 elements in the first dimension and 5 in the second. All elements have a length of 10 characters.
When string values are assigned to a string array, they are padded out with spaces at the end of the string or truncated to the defined length if necessary.
The elements of a string array are identified by the array name followed, in brackets, by one or more numeric values giving the subscript number(s). For example, element a$(2) may be "SMITH" and element b$(12,4) may be "DERBYSHIRE". However, an extra value may be added to define a particular character in a string. In these examples, a$(2,2) would be "M" (the second character in "SMITH"), and b$(12,4,5) would be "Y".
Zero-dimension string arrays
It is possible to create a zero-dimension string array by using only one value in brackets, for example
10 DIM c$(15)
This array has only one element, which is c$, and its length is fixed at the defined value (15 characters).
Format
- DIM letter (num-expr [,num-expr])
- DIM letter$ (num-expr [,num-expr])
See also