Indexing
You
create an index by using the INDEXED
BY
phrase of the OCCURS
clause to identify
an index-name.
For example, INX-A
in the following code
is an index-name:
05 TABLE-ITEM PIC X(8)
OCCURS 10 INDEXED BY INX-A.
The
compiler calculates the value contained in the index as the occurrence
number (subscript) minus 1, multiplied by the length of the table
element. Therefore, for the fifth occurrence of TABLE-ITEM
,
the binary value contained in INX-A
is (5 - 1) *
8, or 32.
You can use an index-name to reference another table only if both table descriptions have the same number of table elements, and the table elements are of the same length.
USAGE IS INDEX
clause to create an index data item, and can use an index data item with any
table. For example, INX-B
in the following
code is an index data item:
77 INX-B USAGE IS INDEX.
. . .
SET INX-A TO 10
SET INX-B TO INX-A.
PERFORM VARYING INX-A FROM 1 BY 1 UNTIL INX-A > INX-B
DISPLAY TABLE-ITEM (INX-A)
. . .
END-PERFORM.
The index-name INX-A
is
used to traverse table TABLE-ITEM
above. The
index data item INX-B
is used to hold the index
of the last element of the table. The advantage of this type of coding
is that calculation of offsets of table elements is minimized, and
no conversion is necessary for the UNTIL
condition.
You can use the SET
statement
to assign to an index data item the value that
you stored in an index-name, as in the statement SET
INX-B TO INX-A
above. For example, when you load records
into a variable-length table, you can store the index value of the
last record into a data item defined as USAGE IS INDEX
.
Then you can test for the end of the table by comparing the current
index value with the index value of the last record. This technique
is useful when you look through or process a table.
SET INX-A DOWN BY 3
The integer represents a number of occurrences. It is converted to an index value before being added to or subtracted from the index.
Initialize
the index-name by using a SET
, PERFORM VARYING
,
or SEARCH ALL
statement. You can then use the index-name
in SEARCH
or relational condition statements. To
change the value, use a PERFORM
, SEARCH
,
or SET
statement.
Because
you are comparing a physical displacement, you can directly use index
data items only in SEARCH
and SET
statements
or in comparisons with indexes or other index data items. You cannot
use index data items as subscripts or indexes.
Subscripting
Putting values into a table
Searching a table
Processing table items using intrinsic functions
Handling tables efficiently
INDEXED BY phrase (Enterprise COBOL for z/OS® Language Reference)
INDEX phrase (Enterprise COBOL for z/OS Language Reference)
SET statement (Enterprise COBOL for z/OS Language Reference)