TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - Index Clause


Use the data-name of the table element, along with a value (called an index) that is added to the address of the table to locate an item (as a displacement from the beginning of the table). This technique is called indexing, or subscripting using index-names.

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.

You can use the 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.

SET Statement:

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.

You can increment or decrement an index-name by an elementary integer data item or a nonzero integer literal, for example:

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.


Example:

Let's see another example to understand the index in the table:

IDENTIFICATION DIVISION.
PROGRAM-ID. CBLHELLO.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
  05 WS-A OCCURS 3 TIMES INDEXED BY I.
    10 WS-B PIC A(2).
    10 WS-C OCCURS 2 TIMES INDEXED BY J.
      15 WS-D PIC X(3).

PROCEDURE DIVISION.
    MOVE '12ABCDEF34GHIJKL56MNOPQR' TO WS-TABLE.
    PERFORM A-PARA VARYING I FROM 1 BY 1 UNTIL I >3
    STOP RUN.

A-PARA.
    PERFORM C-PARA VARYING J FROM 1 BY 1 UNTIL J>2.

C-PARA.
    DISPLAY WS-C(I,J).

Output:

ABC
DEF
GHI
JKL
MNO
PQR


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal