Array Example


Scenario - Declare an array to save to two students information.

Requirement - Let us assume 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----+
      * Single Dimentional Table
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SINDIMTB.
	   AUTHOR. MTH.

       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).

       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 " "

           DISPLAY "First Student Information   => "
           DISPLAY "Roll No:  " WS-ROLL-NO (1)
           DISPLAY "Student Name:  " WS-NAME (1)
           DISPLAY " "

           DISPLAY "Second Student Information  => " 
           DISPLAY "Roll No:  " WS-ROLL-NO (2).
           DISPLAY "Student Name:  " WS-NAME (2). 

           STOP RUN.

JCL -

//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID             
//*                                                      
//STEP01  EXEC PGM=SINDIMTB                              
//STEPLIB  DD  DSN=MATEPK.COBOL.LOADLIB,DISP=SHR         
//SYSOUT   DD  SYSOUT=*

Output -

Class Information: 001PAWAN Y   002KUMAR Y 

First Student Information   =>
Roll No:  001
Student Name:  PAWAN Y
 
Second Student Information  =>
Roll No:  002
Student 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-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).