Working with Tables

A simple example:

A two dimensional table:

A three dimensional table:

This is a sort-of complex example. It is a simple table but the table is populated by an external source of data.

The goal is to read a dataset and load the values into the table.

This will:

  1. Open the dataset.

  2. Load the dataset into the table.

  3. Close the dataset.

  4. Check if the dataset contains too many values for the size of the table.

This will define the table in Working-Storage:

01  WS-Type-Table-Storage.
    12 WS-Type-Max-Element-Counter   PIC S9(4) COMP VALUE +10.
    12 WS-Type-Occurs-Dep-Counter    PIC S9(4) COMP VALUE ZERO.
    12 WS-Type-Table OCCURS 0 TO 10 TIMES
        DEPENDING ON WS-Type-Occurs-Dep-Counter
        INDEXED BY WS-Type-IDX.
        15 WS-Type     PIC X(3).
           88  INS-HMO         VALUE 'HMO'.
           88  INS-PRIVATE     VALUE 'PRI'.
           88  INS-PPO         VALUE 'PPO'.
           88  INS-AFFORDABLE  VALUE 'AFF'.
           88  INS-MEDICARE    VALUE 'MED'.
        15 WS-Type-Counter  PIC S9(4) COMP.

This will load the dataset into the table and check to make sure it has enough room.

Let's start with the Mainline:

0000-Mainline.
    PERFORM 1000-Begin-Job.
    PERFORM 2000-Process.
    PERFORM 3000-End-Job.
    GOBACK.

1000-Begin-Job.
    PERFORM 1010-Load-Type-Table.

    OPEN INPUT  INFILE.
            ... more code here

And now the actual loading of the table:

 1010-Load-Type-Table.
     OPEN INPUT INSTYPE.
     SET WS-Type-IDX TO +1.
     PERFORM 1015-Load-Type Until WS-InsType-EOF.
     CLOSE INSTYPE.
     PERFORM 1019-Verify-Type-Table.

 1015-Load-Type.
     READ INSTYPE
        AT END SET WS-InsType-EOF TO TRUE
     END-READ.
     IF WS-InsType-Good
        ADD +1 TO
           FD-InsType-Record-Cnt
           WS-Type-Occurs-Dep-Counter
       MOVE InsType-Record TO WS-Type(WS-Type-IDX)
       MOVE ZERO TO WS-Type-Counter(WS-Type-IDX)
       SET WS-Type-IDX UP BY +1
     ELSE
        IF WS-InsType-EOF
           NEXT SENTENCE
        ELSE
           DISPLAY "** ERROR **: 1015-Load-Type"
           DISPLAY "Read INSTYPE Failed."
           DISPLAY "File Status: " WS-InsType-Status
           MOVE +8 TO RETURN-CODE
           GOBACK
        END-IF
     END-IF.

 1019-Verify-Type-Table.
D    DISPLAY "WS-Type-Table: "
D    PERFORM VARYING WS-Type-IDX FROM 1 BY 1
D       UNTIL WS-Type-IDX > WS-Type-Occurs-Dep-Counter
D       DISPLAY WS-Type(WS-Type-IDX)
D    END-PERFORM.
     IF WS-Type-Occurs-Dep-Counter >
        WS-Type-Max-Element-Counter
           DISPLAY "** ERROR **: 1019-Verify-Type-Table"
           DISPLAY "WS table size is too small for file."
           DISPLAY "Increase WS-Type-Table-Storage variables."
           MOVE +9 TO RETURN-CODE
           GOBACK
     END-IF.