Table Index


Table data can be accessed by using index. Index refers the table element as the number of displacement positions from the table starting position. Mostly, Indexes are stored in main memory that increases the accessing speed.

How the Index is declared?


Index is declared with using the INDEXED BY phrase of OCCURS clause followed by index-name. Index variable doesn't require additional declaration in Data Division.

01 WS-CLASS. 
   03 WS-STUDENT  OCCURS 2 TIMES INDEXED BY WS-IDX.
      05 WS-ROLL-NO      PIC X(03).       
      05 WS-NAME         PIC X(10).

How the table accessed using index in program?


Table data item can be accessed by using the index like below -

table-data-item (index-name)

The student details can be accessed using index for the above example is -

WS-STUDENT (WS-IDX)

Index Calculation -

The index-value (starting position of data item) is calculated using the below formulae -

index-value = ((occurrence-number - 1) * occurrence-length) + 1

Therefore, for the second occurrence of WS-STUDENT, the binary value contained in WS-IDX is ((2 - 1) * 13) + 1 which is 14. So when accessing WS-STUDENT (2) retrieves the data starting at 14th byte + 13 bytes. Refer the Diagram below for more understanding -

Memory Storage

How the INDEX is initialized?


Index variable can’t be initialized using MOVE statement or during its declaration. SET statement is mainly used to initialize the Index value. PERFORM VARYING statement can also initialize the index. Without initializing, the results are unpredictable. The lowest possible index value is 1. The index initialization for the above example is -

SET WS-IDX   TO 1.

How the INDEX is incremented/decremented?


SET statement is mainly used to increase or decrease the Index value. PERFORM or SEARCH statements can also increase or decrease the index.

Index increment can be done as follows –

SET WS-IDX UP BY 1.

Index decrement can be done as follows –

SET WS-IDX DOWN BY 1.

Possible Errors (SOC4) -


  • If the index increased beyond the maximum number of occurrences and tries to access the table item with it, that causes the S0C4 error.
  • The same error occurred if the index is not initialized and trying to access the table element with uninitialized index.

Solution: -

Debug the program to verify the index value at the time of program abend. It is always good to validate the index with maximum OCCURS value to avoid the error.

Practical Example -


Scenario - Declaring an index, initialized, incremented and used to navigate the table.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. TBINDEX.

       DATA DIVISION. 
       WORKING-STORAGE SECTION.
	  * Declaring table with index
       01 WS-CLASS.
          03 WS-STUDENT  OCCURS 2 TIMES INDEXED BY WS-IDX.
             05 WS-ROLL-NO      PIC X(03).
             05 WS-NAME         PIC X(10).

       PROCEDURE DIVISION.
      * Initializing index to 1 
		   SET WS-IDX          TO 1.
		   MOVE "001PAWAN Y"   TO WS-STUDENT(WS-IDX).

      * Incrementing index by 1
		   SET WS-IDX          UP BY 1.
		   MOVE "002KUMAR"     TO WS-STUDENT(WS-IDX).

      * Displaying full table using index
           PERFORM VARYING WS-IDX FROM 1 BY 1 UNTIL WS-IDX > 2  
                   DISPLAY "STUDENT - " WS-STUDENT(WS-IDX)
           END-PERFORM.
           STOP RUN.

Output -

STUDENT - 001PAWAN Y 
STUDENT - 002KUMAR