Variable length Tables Example


Scenario - Variable length table declaration, usage, and displaying items in it.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. TBOCCURS.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-STUDENT-CNT        PIC 9(02).
       01 WS-CLASS.
          03 WS-STUDENT  OCCURS 1 TO 5 TIMES
                         DEPENDING ON WS-STUDENT-CNT.
             05 WS-ROLL-NO      PIC 9(03) VALUE 1.
             05 WS-NAME         PIC X(10) VALUE 'STUDENT'.

       PROCEDURE DIVISION.
           ACCEPT WS-STUDENT-CNT.
           DISPLAY "CLASS INFO: " WS-CLASS.
           STOP RUN.

Run JCL -

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

Output -

CLASS INFO: 001STUDENT   001STUDENT   001STUDENT

Explaining Example -

In the above example:

  • It defines a variable WS-STUDENT-CNT to hold the count of students in a class (up to 99 students).
  • It then declares a table WS-CLASS with student records, where the number of occurrences depends on the value stored in WS-STUDENT-CNT which is 03.
  • It accepts the count of students from the run JCL and displays the class information, including roll numbers and default names for each student.