Array Index
Array Index 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
Explaining Example -
In the above example:
- It defines a table WS-CLASS with an index WS-IDX, consisting of student records with roll numbers and names.
- It then initializes the index to 1 and assigns values to the table entries using the index.
- After incrementing the index, it displays the entire table by iterating over the index using a PERFORM loop, displaying each student's information.