Dimensioned Arrays

A dimensioned array is a one- or two-dimensioned structure for data. Elements of the array can be thought of as cells rather then fields.

The most significant difference between dimensioned and dynamic arrays is that the size of a dimensioned array must be assigned at compilation, whereas the size of a dynamic array can vary at run-time according to need. Dimensioned arrays require more storage allocation and are somewhat less flexible; they are, however, more efficient at run-time. Each element in a dimensioned array has a direct pointer; each field in a dynamic array, on the other hand, requires the entire string to be searched through from the start.

For example, suppose a dynamic array contains a thousand attributes. Attribute 999 actually refers to the data between the 998th and 999th attribute marks; therefore, to access attribute 999, the processor has to search through the string until 999 attribute marks are found. If several fields towards the end of the string need to be accessed this way, the run-time of the program can be drastically affected.

If a vector (a 1-dimensional array) were used instead of a dynamic array, however, the processor has to access only a single direct pointer to element number 999 in order to retrieve the data. Dimensioned arrays, therefore, provide a shortcut to the field.

See Format, Data and Expressions for a more complete description of the structure of dimensioned arrays.

Assigning Dimensioned Array Variables (DIM)

Before a dimensioned array can be used, it needs to be declared with its dimensions. The DIMENSION statement declares a dimensioned array. Since the DIMENSION statement is interpreted by the compiler, neither variables nor expressions can be used in a DIMENSION statement.

If the array is to be allocated space in the COMMON area, the COMMON statement can also be used to declare a dimensioned array. Arrays that have been declared with the COMMON statement, however, should not be declared again with a DIMENSION statement.

Converting Strings to Dimensioned Arrays

The statements MATPARSE and MATBUILD were designed for placing the elements of a string into a dimensioned array with MATPARSE and later reading them back into a string with MATBUILD.

MATPARSE and MATBUILD are flexible enough to be applicable to several string formats. Their behavior, therefore, is dependent on the number of delimiters specified in the syntax. In the most common case, if the string is a dynamic array, each attribute can be placed into a separate element of the array by specifying an attribute mark as the single delimiter. For example, if STRING were a dynamic array, the attributes of STRING could be read as elements of dimensioned array ARRAY with:

MATPARSE STRING FROM ARRAY, CHAR(254)

The attribute marks will not appear in any of the elements of the new array. The MATPARSE statement in this example might be thought of as a shortcut for:

FOR I = 1 TO MAXELTS

ARRAY(I) = STRING<I>

NEXT I

with MAXELTS representing the dimensions of the array.

Once the string is read into the array, its elements can be freely read and updated. To write the dimensioned array back into the string, MATBUILD can be used with the same delimiter:

MATBUILD ARRAY FROM STRING, CHAR(254)

Each element of the array will be placed into the string, separated by attribute marks.

MATPARSE and MATBUILD are meant to be used together. The same delimiters used for MATPARSE should be used with MATBUILD to reconstruct the string at the end of processing.

MATREAD and MATWRITE

The MATREAD and MATWRITE statements allow file items to be read from and written directly into dimensioned arrays. MATREAD is equivalent to performing a READ statement and then using MATPARSE with an attribute mark as the delimiter. MATWRITE is equivalent to using MATBUILD with an attribute mark as the delimiter and then performing a WRITE. See Reading and Updating File Items later in this section for more information on reading and writing file items.

The MAT Statement

The MAT statement assigns all elements of a dimensioned array to a single value or to the values of another array. For example, to assign all elements of an array ARRAY to 6, the code might read:

MAT ARRAY = 6

This use of the MAT statement is a shorthand for:

FOR I = 1 TO MAXELTS

ARRAY(I) = 6

NEXT I

 

With MAXELTS representing the maximum dimensions of the array.

To assign the elements of ARRAY to the elements of ARRAY2, the code might read:

MAT ARRAY = MAT ARRAY2

which is a shorthand for:

FOR I = 1 TO MAXELTS

ARRAY(I) = ARRAY2(I)

NEXT I

See Also

Overview of mvBASIC Statements and Functions

Assignment Statements

Intrinsic Functions

Internal Program Control

External Program Control

Sending Output to the Screen and Printer

Terminal Input

Dynamic Array Processing

Generalized String Processing

Reading and Updating File Items

Reading and Writing Tapes or Floppy Disks

Communications

Execution Locks

Compiler Directives

Miscellaneous Statements and Functions

The Error Message Processor