ARRAY Subscript
ARRAY Subscript Example
Scenario - Declaring a subscript, initialized, incremented and used to navigate the table.
Code -
----+----1----+----2----+----3----+----4----+----5----+
IDENTIFICATION DIVISION.
PROGRAM-ID. TBSUBSCR.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CLASS.
03 WS-STUDENT OCCURS 2 TIMES.
05 WS-ROLL-NO PIC X(03).
05 WS-NAME PIC X(10).
* Declaring a subscript
01 WS-SUB PIC S9(04) COMP.
PROCEDURE DIVISION.
* Initializing the subscript to 1
MOVE 1 TO WS-SUB.
MOVE "001PAWAN Y" TO WS-STUDENT (WS-SUB).
* Incrementing subscript by 1
COMPUTE WS-SUB = WS-SUB + 1.
MOVE "002KUMAR" TO WS-STUDENT (WS-SUB).
* Displaying full table using subscript
PERFORM VARYING WS-SUB FROM 1 BY 1 UNTIL WS-SUB > 2
DISPLAY "STUDENT" WS-SUB " - " WS-STUDENT(WS-SUB)
END-PERFORM.
STOP RUN.
Run JCL -
//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID //* //STEP01 EXEC PGM=TBSUBSCR //STEPLIB DD DSN=MATEPK.COBOL.LOADLIB,DISP=SHR //SYSOUT DD SYSOUT=*
Output -
STUDENT0001 - 001PAWAN Y STUDENT0002 - 002KUMAR
Explaining Example -
In the above example:
- It initializes a table WS-CLASS with two occurrences of student records, each containing roll numbers and names.
- It also declares a subscript WS-SUB to iterate over the table.
- The code initializes the subscript to 1, assigns values to the table entries using the subscript, increments the subscript, assigns values to the next entry, and finally displays the entire table by iterating over the subscript using a PERFORM loop.