Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 38 ยท Tables and Arrays

Multi-Dimensional and Variable-Length Tables

Build two- and three-level COBOL tables with nested OCCURS, address them with several subscripts, and use OCCURS DEPENDING ON for variable-length tables and records.

Real data is often a grid: sales by region and month, scores by student and exam, rooms by floor and building. COBOL handles this by nesting OCCURS clauses, up to seven levels deep in the 2002 standard (three in COBOL-74). A second feature, OCCURS ... DEPENDING ON, lets a table have a different number of entries at run time, which matters for variable-length records.

Two-dimensional tables

Put an OCCURS inside a group that itself has an OCCURS:

COBOL
       01  REGION-SALES.
           05  REGION OCCURS 4 TIMES.
               10  MONTH-AMOUNT PIC 9(6)V99 OCCURS 12 TIMES.

You can read this as "4 regions, each with 12 monthly amounts". The first subscript selects the region and the second the month:

COBOL
           MOVE 1250.00 TO MONTH-AMOUNT (2, 7)
           ADD MONTH-AMOUNT (2, 7) TO REGION-TOTAL

MONTH-AMOUNT (2, 7) is region 2, month 7. The order of subscripts matches the order of the OCCURS levels from the outside in. Spaces after the comma are customary. Memory holds all 12 months of region 1, then all of region 2, and so on, so the table needs 4 x 12 x 8 = 384 bytes.

flowchart TD
    T["REGION-SALES"] --> R1["REGION (1)"]
    T --> R2["REGION (2)"]
    T --> R3["REGION (3) / (4)"]
    R1 --> M11["MONTH-AMOUNT (1,1) ... (1,12)"]
    R2 --> M21["MONTH-AMOUNT (2,1) ... (2,12)"]

A two-level table is a table of tables.

You may subscript a group level as well: REGION (3) refers to all twelve amounts of region 3 as one 96-byte group, so MOVE ZERO TO REGION (3) is not safe for numerics (use INITIALIZE REGION (3)).

Nested loops

Walking such a table takes one loop per dimension. The classic report is a total per region and a grand total:

COBOL
           PERFORM VARYING R FROM 1 BY 1 UNTIL R > 4
               MOVE ZERO TO REGION-TOTAL
               PERFORM VARYING M FROM 1 BY 1 UNTIL M > 12
                   ADD MONTH-AMOUNT (R, M) TO REGION-TOTAL
               END-PERFORM
               DISPLAY "Region " R " total " REGION-TOTAL
               ADD REGION-TOTAL TO GRAND-TOTAL
           END-PERFORM

COBOL also has PERFORM VARYING ... AFTER ... for the same job in a single statement:

COBOL
           PERFORM VARYING R FROM 1 BY 1 UNTIL R > 4
                     AFTER M FROM 1 BY 1 UNTIL M > 12
               ADD MONTH-AMOUNT (R, M) TO GRAND-TOTAL
           END-PERFORM

R is the outer variable and M the inner; M runs through all its values for each value of R. The AFTER form is compact, but when you need work between loops (like printing a region heading) nested PERFORMs are clearer.

Python Stand-in: region by month totals (Decimal, 1-based)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each region adds 12 x (r x 100 + 0.25) + 5 x 78, so the totals are 1,593.00, 2,793.00, 3,993.00 and 5,193.00, and the grand total is 13,572.00.

Three levels and group subscripts

A third level follows the same pattern, for instance BUILDING > FLOOR > ROOM. Three subscripts then appear in each reference: ROOM-RATE (B, F, R). Beyond three levels, consider whether a file or a different design is better; deeply nested tables are hard to read and test.

When a lower level has no name that is useful by itself you can use FILLER, but any item you refer to in the Procedure Division needs a real name.

Indexes on several levels

Each OCCURS level can have its own index:

COBOL
           05  REGION OCCURS 4 TIMES INDEXED BY R-IX.
               10  MONTH-AMOUNT PIC 9(6)V99 OCCURS 12 TIMES
                       INDEXED BY M-IX.

You then write MONTH-AMOUNT (R-IX, M-IX). A SEARCH over an inner level varies only the index of that level, so you set the outer index first. See indexes and SEARCH.

OCCURS DEPENDING ON

A fixed OCCURS 100 TIMES wastes space when most records need only 3 entries. OCCURS ... DEPENDING ON declares a maximum and tells the program how many entries are currently in use:

COBOL
       01  ORDER-RECORD.
           05  ORDER-NO      PIC 9(6).
           05  LINE-COUNT    PIC 99.
           05  ORDER-LINE OCCURS 1 TO 50 TIMES
                   DEPENDING ON LINE-COUNT.
               10  ITEM-CODE PIC X(6).
               10  QTY       PIC 9(3).
  • LINE-COUNT holds the current number of lines. It must be set before you use the table, and it must stay between 1 and 50 (or 0 to 50 in COBOL 2002 and later when you write 0 TO 50).
  • The table's length for operations such as MOVE of the whole group, or WRITE of the record, is based on the current value of LINE-COUNT.
  • The storage is still reserved for the maximum; the saving is in files. A variable-length record is written with only LINE-COUNT entries.

Rules of thumb for DEPENDING ON: put the counter before the table, never put another variable-length item after it in the same record, and never change the counter while you still need the old data. Changing it silently re-slices the record.

Reading a variable-length record

When you read a record of this type, the counter arrives with the data, so the loop is bounded by it:

COBOL
           PERFORM VARYING L FROM 1 BY 1 UNTIL L > LINE-COUNT
               DISPLAY ITEM-CODE (L) " x " QTY (L)
           END-PERFORM

Variable-length records are common on mainframes (RECFM=V); file handling shows how such files are declared.

Python Stand-in: a variable-length record packed into fixed-width text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The record lengths are 17 and 35: the record is only as long as the data in it.

Common mistakes

  1. Swapped subscripts. MONTH-AMOUNT (M, R) compiles when both fields are numeric, but reads the wrong cell or runs outside the table. Name the subscripts R and M clearly, or use indexes called R-IX and M-IX.
  2. Counter not initialized. With DEPENDING ON, an uninitialized counter holds garbage and the group length becomes unpredictable.
  3. Table bigger than 16 MB. Compilers cap the size of one item (Enterprise COBOL: 128 MB, but practical limits are lower). Large lookups belong in a file or database.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In MONTH-AMOUNT (2, 7), which subscript is the outer table?

    Subscripts follow the OCCURS levels from the outermost to the innermost, so 2 selects the region and 7 the month.
  2. 2. What does OCCURS 1 TO 50 TIMES DEPENDING ON LINE-COUNT do?

    Storage is reserved for the maximum, but the logical length of the record follows the counter.
  3. 3. Which statement walks all cells of R x M with one PERFORM?

    AFTER adds an inner loop variable that cycles fastest.
  4. 4. Why is MOVE ZERO TO REGION (3) risky?

    A group move ignores the field types inside, so use INITIALIZE to set each numeric field properly.

Summary

  • Nest OCCURS for grids; subscripts follow the levels from outer to inner.
  • Use nested PERFORM VARYING or PERFORM VARYING ... AFTER to visit every cell.
  • Give each level its own INDEXED BY name when you plan to use SEARCH.
  • OCCURS ... DEPENDING ON defines a maximum size and makes records variable in length; initialize and protect the counter.

Next: Sequential files