TExcelTAD
Home › COBOL › COBOL Multi-dimensional Tables

COBOL Multi-dimensional Tables

A table element can itself contain an OCCURS clause. That gives a two-dimensional table (rows and columns), and COBOL allows up to seven levels of nesting (GnuCOBOL allows more). Each level needs its own subscript, written in order from the outermost to the innermost: WS-CELL (row, column).

Declaring a 2-D table

Sales for 3 stores over 4 quarters:

cobol
       01  WS-SALES-TABLE.
           05  WS-STORE           OCCURS 3 TIMES.
               10  WS-STORE-NAME  PIC X(8).
               10  WS-QUARTER     PIC 9(5) OCCURS 4 TIMES.

WS-QUARTER (2, 4) is store 2, quarter 4. WS-STORE-NAME (2) needs only one subscript because it belongs to the outer level. In memory the table is laid out row by row: store 1's name and four quarters, then store 2, and so on.

Totals by row and by column

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SALES-GRID.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-SALES-DATA.
           05  FILLER     PIC X(28)
               VALUE "North   01200013000110001500".
           05  FILLER     PIC X(28)
               VALUE "South   00900009500100001200".
           05  FILLER     PIC X(28)
               VALUE "West    01500014000160001800".
       01  WS-SALES-TABLE REDEFINES WS-SALES-DATA.
           05  WS-STORE           OCCURS 3 TIMES.
               10  WS-STORE-NAME  PIC X(8).
               10  WS-QUARTER     PIC 9(5) OCCURS 4 TIMES.
       01  WS-S                   PIC 9.
       01  WS-Q                   PIC 9.
       01  WS-ROW-TOTAL           PIC 9(6).
       01  WS-COL-TOTAL           PIC 9(6) OCCURS 4 TIMES VALUE 0.
       01  ED-NUM                 PIC ZZZ,ZZ9.
       PROCEDURE DIVISION.
           PERFORM VARYING WS-S FROM 1 BY 1 UNTIL WS-S > 3
               MOVE 0 TO WS-ROW-TOTAL
               DISPLAY WS-STORE-NAME (WS-S) WITH NO ADVANCING
               PERFORM VARYING WS-Q FROM 1 BY 1 UNTIL WS-Q > 4
                   ADD WS-QUARTER (WS-S, WS-Q) TO WS-ROW-TOTAL
                                                WS-COL-TOTAL (WS-Q)
                   MOVE WS-QUARTER (WS-S, WS-Q) TO ED-NUM
                   DISPLAY ED-NUM WITH NO ADVANCING
               END-PERFORM
               MOVE WS-ROW-TOTAL TO ED-NUM
               DISPLAY " |" ED-NUM
           END-PERFORM
           DISPLAY "Total   " WITH NO ADVANCING
           PERFORM VARYING WS-Q FROM 1 BY 1 UNTIL WS-Q > 4
               MOVE WS-COL-TOTAL (WS-Q) TO ED-NUM
               DISPLAY ED-NUM WITH NO ADVANCING
           END-PERFORM
           DISPLAY " "
           STOP RUN.
North     1,200  1,300  1,100  1,500 |  5,100
South       900    950  1,000  1,200 |  4,050
West      1,500  1,400  1,600  1,800 |  6,300
Total     3,600  3,650  3,700  4,500

The inner loop varies the column fastest, which follows the memory layout. WS-COL-TOTAL is declared with OCCURS … VALUE 0, which GnuCOBOL accepts to set every element to zero.

A lookup grid

Two-dimensional tables are often used for rate tables: for example, a shipping price by weight band and zone. The values can be loaded from one long literal:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SHIPPING-RATES.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
      *> 3 weight bands x 4 zones, each price PIC 99V99
       01  WS-RATE-DATA       PIC X(48) VALUE
           "0450055007000900" &
           "0600075009501250" &
           "0900110014001800".
       01  WS-RATE-TABLE REDEFINES WS-RATE-DATA.
           05  WS-BAND            OCCURS 3 TIMES.
               10  WS-PRICE       PIC 99V99 OCCURS 4 TIMES.
       01  WS-WEIGHT-KG           PIC 99V9  VALUE 3.2.
       01  WS-ZONE                PIC 9     VALUE 3.
       01  WS-B                   PIC 9.
       PROCEDURE DIVISION.
           EVALUATE TRUE
               WHEN WS-WEIGHT-KG <= 1   MOVE 1 TO WS-B
               WHEN WS-WEIGHT-KG <= 5   MOVE 2 TO WS-B
               WHEN OTHER               MOVE 3 TO WS-B
           END-EVALUATE
           DISPLAY "Parcel " WS-WEIGHT-KG " kg to zone " WS-ZONE
           DISPLAY "Band " WS-B ", price " WS-PRICE (WS-B, WS-ZONE)
           STOP RUN.
Parcel 03.2 kg to zone 3
Band 2, price 09.50

Three dimensions

A third level works the same way. Here, temperature readings for 2 cities × 2 days × 3 times of day, filled with nested loops and a formula:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. THREE-D.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-READINGS.
           05  WS-CITY            OCCURS 2 TIMES.
               10  WS-DAY         OCCURS 2 TIMES.
                   15  WS-TEMP    PIC S99 OCCURS 3 TIMES.
       01  WS-C                   PIC 9.
       01  WS-D                   PIC 9.
       01  WS-T                   PIC 9.
       PROCEDURE DIVISION.
           PERFORM VARYING WS-C FROM 1 BY 1 UNTIL WS-C > 2
               AFTER WS-D FROM 1 BY 1 UNTIL WS-D > 2
               AFTER WS-T FROM 1 BY 1 UNTIL WS-T > 3
                   COMPUTE WS-TEMP (WS-C, WS-D, WS-T) =
                       WS-C * 10 + WS-D * 2 + WS-T - 8
           END-PERFORM
           DISPLAY "City 2, day 1, evening: " WS-TEMP (2, 1, 3)
           DISPLAY "City 1, day 2, morning: " WS-TEMP (1, 2, 1)
           DISPLAY "Bytes in table: " FUNCTION LENGTH(WS-READINGS)
           STOP RUN.
City 2, day 1, evening: +17
City 1, day 2, morning: +07
Bytes in table: 24

Tip: Subscripts can be written WS-TEMP (WS-C WS-D WS-T) with spaces only, or with commas. Commas are easier to read. A subscript can also be a relative expression like WS-SCORE (WS-I + 1).

Test yourself

With 05 WS-ROW OCCURS 3. 10 WS-CELL PIC 9 OCCURS 4., how many cells are there?

  1. 3
  2. 4
  3. 7
  4. 12

How many subscripts does WS-CELL need in that table?

  1. One
  2. Two: row, then cell
  3. Two: cell, then row
  4. None

In what order are the elements of a 2-D COBOL table stored?

  1. Column by column
  2. Row by row (the last subscript varies fastest)
  3. Randomly
  4. In sorted order