Arrays

From CometWiki

Jump to: navigation, search

Arrays (subscripted variables)

Internet Basic supports arrays (subscripted variables) for both numeric and string variables. Arrays are defined via the LOCAL and COMMON statements in the Data Division of the program. While there is no theoretical limit to the number of dimensions in an array, an array may contain a maximum of 32K bytes.

The following example shows how to define a one-dimensional string variable named LIST$. This array contains 200 elements, each with a length of 20 bytes:

LENGTH 20             ! Length of 20 bytes
LOCAL LIST$(200)      ! Array variable with 200 elements

In the Prodecure Division, an array variable is referred to using a numeric constant or numeric variable as the array subscript. The first subscript is 1.

For example:

LIST$(1) = "HELLO"     ! numeric constant as subscript

or

FOR I = 1 TO 200
LIST$(I) = "HELLO"     ! numeric variable as subscript
NEXT I

Note: When a variable subscript is used, Comet verifies that the calculated offset for the array element is within the total array space only. It does not check to see if the size of any particular dimension has been exceeded. If the calculated offset falls beyond the array space, Comet issues an exception 50 (ARRAY SUBSCRIPT OUT OF RANGE). Next, we'll expand the LIST$ variable to a two-dimensional variable:

LENGTH 20             ! Length of 20 bytes
LOCAL LIST$(200,2)    ! Array variable with 200 x 2 elements

Here's a three-dimensional array named CHART$, with 10 x 5 x 8 dimensions, each element defined as a 15-byte string:

LENGTH 15             ! Length of 15 bytes
LOCAL CHART$(10,5,8)  ! Array variable with 10 x 5 x 8 elements

Finally, here's six-dimensional numeric array named DATA, with 4 x 4 x 4 x 4 x 4 x 4 dimensions, each element defined as length/precision of 2.0:

LENGTH 2.0               ! Length of 2.0 digits
LOCAL DATA(4,4,4,4,4,4)  ! Array variable with 4 x 4 x 4 x 4 x 4 x 4 elements

Note: While all of the above examples show LOCAL variables, they could also be defined as COMMON variables.

Personal tools