COBOL Manual for TNS and TNS/R Programs

Data Fundamentals
HP COBOL Manual for TNS and TNS/R Programs522555-006
4-21
Subscripts
Number and Range of Subscripts
HP COBOL supports subscripting of up to 7 dimensions.
The lowest legitimate subscript value is 1, which selects the first element of a table.
The next sequential elements of the table are selected by subscripts whose values are
2, 3, and so on. The highest subscript value, in any particular case, is the maximum
number of elements in the table. Any higher subscript is erroneous, and can cause an
error.
You can use the directive CHECK, with a level-number greater than 1, to include code
in the resulting program to perform range checking during execution. If the program
attempts to use a subscript that is out of range, the range checking routine reports a
fatal error.
If you do not include a CHECK directive, the compilation produces a program in which
subscript-out-of-range errors go undetected. These undetected errors can cause
corruption of other data, producing errors that can be difficult to locate, even with a
symbolic debugger.
Using Indexes Instead of Subscripts
Using an index can be more efficient than using a qualified-name subscript because an
index’s value is the actual byte-offset of a table item within the table. A qualified-name
subscript’s value (expressed as an occurrence number) must first be converted to a
byte-offset value, usually by a multiplication operation.
To convert an index value to an occurrence number (or to do the reverse operation),
use the SET statement to copy the occurrence number associated with an index-name
to a data item (or the other way around).
A data item described by a USAGE INDEX clause provides a place to store the value
of an index (from an index-name) without converting it to an occurrence number. Use
the SET statement to copy the value to or from the index data item. The value stored in
this fashion is not an occurrence number and cannot be used with another table.
The only statements that can initialize or modify an index are the SET, SEARCH
(including SEARCH ALL), or PERFORM statements.
Example 4-7. Subscripting for Tables
MOVE TOTAL OF REPORT-MARK (8) TO REPORT-TOTAL-8.
MOVE MONTH-NAME (MONTH-NUMBER + 2) TO REPORT-MONTH.
MOVE MATRIX (ROW COLUMN) TO OUTPUT-DISPLAY-LINE.