Arrays


ANSI

 

Arrays provide a table method for processing DIM, FORM, or INTEGER variables and objects. The actual usage of arrays is described in the sections outlining the variables or objects involved. To reference individual array items within a program, it is necessary to follow the Data Definition Label with the specific array element desired. This may be accomplished by using a decimal constant, a FORM variable or an INTEGER variable enclosed within parenthesis. However, some instructions process all array items if no particular item is specified (the instructions that do so are indicated).

Note the following:

 

MOVE

ARRAY(12) TO OUTPUT

 

In this example, the twelfth element of the variable ARRAY would be moved into the variable OUTPUT. If ARRAY were only defined as having ten elements, a compiler error would occur. You may not reference an element outside the defined range of the array.

 

 

MOVE

ARRAY(INDEX) TO OUTPUT

 

This example uses the value in INDEX (that must be a valid FORM, INTEGER, or an EQUATEd value) to manipulate the appropriate array element. If INDEX were a FORM variable, digits beyond the decimal would be truncated (only the whole number is used). If INDEX contained a value greater than the number of elements defined for ARRAY, an F02 (format) execution time error would occur.

 

Multi-dimension arrays are supported as outlined in the appropriate data definition section. Referencing a multi-dimension array is identical to a single dimension array, except that each dimension must be specifically referenced. For example:

 

 

MOVE

ARRAY(2,3) TO OUTPUT

 

The ARRAY item corresponding to first dimension 2, second dimension 3 is moved into OUTPUT.

 

 

MOVE

ARRAY(NDX1,NDX2) TO OUTPUT

 

NDX1 is a numeric variable specifying the first dimension, while NDX2 is a numeric variable specifying the second dimension.

 

When multi-dimension arrays are initialized, the process begins at the rightmost dimension (lowest array level) and ends with the leftmost dimension (highest array level). For example:

ARRAY

DIM

5(2,3),("A"),("B"),("C"):

 

 

        ("D"),("E"),("F")

 

After initialization: ARRAY(1,1) equals A, ARRAY(1,2) equals B, ARRAY(1,3) equals C, ARRAY(2,1) equals D, ARRAY(2,2) equals E, and ARRAY(2,3) equals F.

 

Arrays may have labels on each individual element as follows:

 

ARR1

DIM

20(3):

A1

 

("ABC"):

A2

 

("DEF"):

A3

 

("XXX")

 

The compiler supports arrays (single and multi-dimensional) that are declared with a range of allowable index values. The index range values can be between zero (0) and 65,535. When an array declaration is specified without the {min} range value, the default value is one (1). The syntax for an array range is:

 

{label}

DIM

nnn({min}..{max}[,{min1}..{max1}[,....]])

 

{min} is the index minimum value that is the lowest allowed index value. {max} is the index maximum that is the highest allowed index value. An example of an array range is:

 

ARR1

DIM

3(0..4)

 

This statement defines a five (5) element array that is accessed using index values of 0, 1, 2, 3, and 4. Any other index values will cause a FORMAT error.

 

ARR2

FORM

2(201..210)

 

This statement defines a ten (10) element array that is accessed using index values of 201 through 210.

 

Various PL/B instructions allow use of entire arrayed variables with or without specifying a particular array element. If a particular array element is specified, it alone is processed. If not, all array items are processed. See each instruction for the action taken when individual array elements are not specified. When working with multi-level arrays, if the right index values are not specified, a partial array operation may be performed. On all I/O instructions (KEYIN, DISPLAY, PRINT, READ, WRITE, etc.) that do not specify an array element, the entire array is used.

 

Anytime a data type is referenced, an array item may be used unless specifically noted to the contrary.

 

 

See Also: Data Definitions

 



PL/B Language Reference Global Data Items Pointers