Compaq COBOL AAQ2G1FTK User Manual

Page 137

Advertising
background image

Handling Tables

4.3 Accessing Table Elements

You cannot access index items as normal data items; that is, you cannot use them,
redefine them, or write them to a file. However, the SET statement can change
their values, and relation tests can examine their values. The index integer you
specify in the SET statement must be in the range of one to the integer value
in the OCCURS clause. The sample MOVE statement shown in Example 4–17
moves the contents of the third element of A-GROUP to I-FIELD.

Example 4–17 Subscripting with Index Name Items

Table Description:

01 A-TABLE.

03 A-GROUP OCCURS 5 TIMES

INDEXED BY IND-NAME.
05

ITEMC

PIC X

VALUE "C".

05

ITEMD

PIC X

VALUE "D".

01 I-FIELD

PIC X(5).

Procedural Instructions:

SET IND-NAME TO 3.
MOVE A-GROUP(IND-NAME) TO I-FIELD.

Note

Compaq COBOL initializes the value of all indexes to 1. Initializing
indexes is an extension to the ANSI COBOL standard. Users who write
COBOL programs that must adhere to standard COBOL should not rely
on this feature.

4.3.5 Relative Indexing

To perform relative indexing when referring to a table element, you follow the
index name with a plus or minus sign and an integer literal. Although it is
easy to use, relative indexing generates additional overhead each time a table
element is referenced in this way. The run-time overhead for relative indexing of
variable-length tables is significantly greater than that required for fixed-length
tables. If any of the range checks reveals an out-of-range index value, program
execution terminates, and an error message is issued. You can use the

-check

flag (on Tru64 UNIX systems) or the /CHECK qualifier (on OpenVMS Alpha
systems) to check the range when you compile the program.

On Tru64 UNIX, see Chapter 1 or the cobol man page for more information about
the

-check

flag.

On OpenVMS, invoke the online HELP facility for Compaq COBOL at the
OpenVMS Alpha system prompt for more information about the /CHECK
qualifier.

The following sample MOVE statement moves the fourth repetition of A-GROUP
to I-FIELD:

SET IND-NAME TO 1.
MOVE A-GROUP(IND-NAME + 3) TO I-FIELD.

Handling Tables 4–15

Advertising