Single Dimension Array with Subscript
Single Dimension Array with Subscript Example
Scenario - Accessing single dimensional array using subscript.
Requirement - Let us declare a table to process two student details. WS-CLASS is the group variable and WS-STUDENT is a variable with student information OCCURS 2 times to capture the two students information.
Code -
----+----1----+----2----+----3----+----4----+----5----+
* Accessing Single Dimentional Table using subscript
IDENTIFICATION DIVISION.
PROGRAM-ID. SINDIMIX.
AUTHOR. MTH.
DATA DIVISION.
WORKING-STORAGE SECTION.
* Declaring the array
01 WS-CLASS.
03 WS-STUDENT OCCURS 2 TIMES.
05 WS-ROLL-NO PIC X(03).
05 WS-NAME PIC X(10).
* Declaring the subscript for the array
01 WS-SUBSCR1 PIC S9(04) COMP.
PROCEDURE DIVISION.
* Populating array occurrences
MOVE "001PAWAN Y" TO WS-STUDENT(1).
MOVE "002KUMAR Y" TO WS-STUDENT(2).
DISPLAY "Class Information: " WS-CLASS.
DISPLAY " "
* Initializing Subscript
MOVE 1 TO WS-SUBSCR1.
DISPLAY "Students Information => "
* Loop for displaying two students information
PERFORM UNTIL WS-SUBSCR1 > 2
DISPLAY "Roll No: " WS-ROLL-NO (WS-SUBSCR1)
DISPLAY "Name: " WS-NAME (WS-SUBSCR1)
* Increasing the subscript by 1
ADD 1 TO WS-SUBSCR1
END-PERFORM.
STOP RUN.
JCL -
//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID //* //STEP01 EXEC PGM=SINDIMIX //STEPLIB DD DSN=MATEPK.COBOL.LOADLIB,DISP=SHR //SYSOUT DD SYSOUT=*
Output -
Class Information: 001PAWAN Y 002KUMAR Y Students Information => Roll No: 001 Name: PAWAN Y Roll No: 002 Name: KUMAR Y
Explaining Example -
In the above example:
- WS-STUDENT(1) represents first student information and WS-STUDENT(2) represents second student information.
- WS-SUBSCR1 is subscript declared for WS-STUDENT single dimensional array. The subscript is initialized using MOVE statement and incremented using ADD statement.
- WS-ROLL-NO(1), WS-NAME(1) represents first student roll number and name. similarly, WS-ROLL-NO(2), WS-NAME(2) represents the second student roll number and name.
- WS-CLASS represents the full class informaton. i.e., two students information - WS-STUDENT(1) and WS-STUDENT(2).