Dim

From BR Wiki
Revision as of 15:37, 24 April 2014 by Laura (talk | contribs) (→‎Syntax)
Jump to navigation Jump to search

The Dim (DI) statement defines the sizes (also known as Dimensions) of arrays or matrices and the maximum length of string variables.

Comments and Examples

The three main branches of the syntax diagram correspond to three different uses of the DIM statement. All three may be intermixed in a single DIM statement.

One function of DIM is to define the size of numeric matrices, as in the following example:

100 DIM TOTALS(10,20)

Two types of information are indicated inside the parentheses in this example. First, the magnitude of the numbers is indicated by the upper-limit value for each dimension. Assuming Option Base 1 (see the Technical Considerations section below), the first dimension has 10 elements and the second dimension has 20 elements. Second, the number of dimensions is indicated by the number of numbers (equal to the number of commas plus 1) inside the parentheses. The above example has two dimensions: 10 are the number of elements in a row, and 20 are the number of elements in a column.

A second function of DIM, as described in the bottom path of the syntax diagram, is to set the maximum length of a string variable. The default is a maximum length of 18 characters. To use any string longer than 18 characters, the variables must appear in a DIM statement that assigns a longer length. The following statement increases the maximum string length of the three string variables to 30, 25, and 20 characters, respectively:

00110 DIM NAME$*30,ADDR$*25,CITY$*20

The third function of DIM, as described in the middle path of the syntax diagram, is a combination of the first two. DIM can set the size (dimensions) and string length of a string array. The following example allocates space for a string array called CODES$ with 25 elements (assuming Option Base 1), each having a maximum length of one character (saving memory space over the default):

00120 DIM CODES$(25)*1

Syntax

DIM {[<string array> (<rows>,{<columns>,})*<length>]|[<numeric array> (<rows>,{<columns>,})]|[<string variable>*<length>],}

Defaults

1.) One dimensional array.
2.) Maximum string variable length of 18 characters.

Parameters

In the top path of the syntax diagram, "numeric array" is any valid numeric variable array name. The numbers inside the parentheses ("rows" and "columns") are integer constants that indicate the maximum size for each dimension. At least one dimension (corresponding to "rows") is required. Business Rules up to 7 dimensions; thus, "columns" is optional and can be repeated.

In the middle path of the syntax diagram, "string array" is any string variable array name. The numbers inside the parentheses (e.g., "rows", "columns") are used as for "num-array", explained above. The optional "length" parameter sets the maximum length of all elements in the string array.

In the bottom path of the syntax diagram, "string variable" is any valid string variable name. The optional "length" parameter sets the maximum length of the string.

Technical Considerations

  1. The number of elements in an array is partly determined by the BASE specified in the OPTION statement. Business Rules defaults to Option Base 1, which means that the first element of each array is 1. The other possibility is OPTION BASE 0, which makes 0 the first element of each dimension of all arrays. Thus, DIM Q(20) defaults to 20 elements, but could have 21 elements if an "OPTION BASE 0" statement were included.
  2. The same variable name can be used for a single variable and an array. The same program could contain A and A(1), as well as A$ and A$(1).
  3. If a DIM statement is omitted, Business Rules defaults to a one-dimensional array of 10 elements. For example, 250 INPUT A(5) allocates space for data as if DIM A(10) had been coded. A matrix must be explicitly declared if it requires more than 10 elements, strings longer than 18 characters, or more than one dimension.
  4. Zero is the initial value of each numeric array element; each string array element is initialized to the null string (zero length). Initialization takes place when the program is started.
  5. Although an array can be declared in a DIM statement only once in a program, the MAT statement allows redimensioning of an array to decrease dimensions (to save space) or increase the dimensions.
  6. Arrays need not be dimensioned the same when chaining between programs, although this is recommended for program readability. When array dimensions do not match, those in the chained-from program overrule the ones in the chained-to program. (See also the UDIM function, which may be used to determine and/or Verify array size.)
  7. Dim must appear by itself on a line. Other statements may come neither before nor after it on the line.
  8. When the DIM statement is deleted, string lengths and dimensions resume their default values.

See Also