Multi-dimensional tables
A table inside a table is a grid, and COBOL writes it the way a reader would: a row that repeats, and inside each row a cell that repeats. The subscripts come in the same order as the declarations, outer first.
A table inside a table
The outer OCCURS sits on a group, the inner one on the entries of that group. Three rows of four cells is a level 05 that repeats three times with a level 10 inside it that repeats four:
IDENTIFICATION DIVISION.
PROGRAM-ID. GRID.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 GRID-T.
05 ROW-E OCCURS 3 TIMES.
10 CELL-E PIC 9(3) OCCURS 4 TIMES.
01 R-F PIC 9 VALUE 0.
01 C-F PIC 9 VALUE 0.
01 TOTAL-F PIC 9(5) VALUE 0.
PROCEDURE DIVISION.
PERFORM VARYING R-F FROM 1 BY 1 UNTIL R-F > 3
PERFORM VARYING C-F FROM 1 BY 1 UNTIL C-F > 4
COMPUTE CELL-E(R-F, C-F) = R-F * 10 + C-F
END-PERFORM
END-PERFORM.
PERFORM VARYING R-F FROM 1 BY 1 UNTIL R-F > 3
DISPLAY "row " R-F ":" WITH NO ADVANCING
PERFORM VARYING C-F FROM 1 BY 1 UNTIL C-F > 4
DISPLAY " " CELL-E(R-F, C-F) WITH NO ADVANCING
ADD CELL-E(R-F, C-F) TO TOTAL-F
END-PERFORM
DISPLAY " "
END-PERFORM.
DISPLAY "total " TOTAL-F.
DISPLAY "length " LENGTH OF GRID-T.
STOP RUN.
cobc -x -free -Wall -o grid grid.cob
./grid
row 1: 011 012 013 014
row 2: 021 022 023 024
row 3: 031 032 033 034
total 00270
length 36
Two loops for a grid
The outer loop moves the row and the inner one the cell, and the inner counter is reset to one on every row because its PERFORM VARYING is reached again. The subscript list (R-F, C-F) follows the declaration: the row first, then the cell in that row.
The display builds each row with WITH NO ADVANCING and ends it with an empty DISPLAY, which is the prompt technique of the lesson on ACCEPT and DISPLAY used for a report line. The trailing space in the output is the one that the empty display ended.
The order of the bytes
The grid is not a pointer to rows: it is twelve cells laid out one after the other, row by row. A second description of the same bytes as a flat table of six shows the order, and it is the same device the lesson on REDEFINES introduced:
IDENTIFICATION DIVISION.
PROGRAM-ID. FLAT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 GRID-T.
05 ROW-E OCCURS 2 TIMES.
10 CELL-E PIC 9(2) OCCURS 3 TIMES.
01 FLAT-T REDEFINES GRID-T.
05 FLAT-E PIC 9(2) OCCURS 6 TIMES.
01 I-F PIC 9 VALUE 0.
PROCEDURE DIVISION.
MOVE 11 TO CELL-E(1, 1).
MOVE 12 TO CELL-E(1, 2).
MOVE 13 TO CELL-E(1, 3).
MOVE 21 TO CELL-E(2, 1).
MOVE 22 TO CELL-E(2, 2).
MOVE 23 TO CELL-E(2, 3).
PERFORM VARYING I-F FROM 1 BY 1 UNTIL I-F > 6
DISPLAY "flat " I-F " = " FLAT-E(I-F)
END-PERFORM.
STOP RUN.
cobc -x -free -Wall -o flat flat.cob
./flat
flat 1 = 11
flat 2 = 12
flat 3 = 13
flat 4 = 21
flat 5 = 22
flat 6 = 23
The three cells of the first row come first, then the three of the second. A program that writes the grid to a file writes that sequence, and a program that reads a file of six fixed cells into the grid fills it row by row.
How many bytes
LENGTH OF GRID-T is 36 in the first program: three rows of four cells of three digits, with nothing added between rows and nothing added between cells. The size is the product of the dimensions and the width of a cell, which is what makes a grid cheap in a copybook and expensive in a report: a table of a thousand rows of a hundred cells of ten bytes is a megabyte of working storage.
The next lesson puts a record in each cell instead of a number, which is the shape of a file read into memory. The same two subscripts then name a field of one entry.
Exercises
- Change the grid to four rows of three cells and print it again. What is LENGTH OF now?
- Fill the grid with the column number in the hundreds and the row in the units, and print a column at a time.
- Search the grid for the value 23 with two loops and print the two subscripts you found.
- Swap the two loops of the printing paragraph and keep the same subscripts. What does the output look like now?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
In what order does a two-dimensional table store its cells?
Answer
Row by row: every cell of the first row, then the second
The flat redefinition printed the three cells of row one before any cell of row two.
-
How are the subscripts of CELL-E(R, C) ordered?
Answer
Outer first, then inner, as the declarations nest
The row is the outer OCCURS and the cell the inner one, and the subscript list follows that order.
-
What is LENGTH OF for a grid of 3 rows of 4 cells of PIC 9(3)?
Answer
36 bytes
Twelve cells of three bytes are thirty-six bytes, which the program printed.
-
What does the flat redefinition prove?
Answer
The grid and the flat table are the same bytes in the same order
The six cells of the flat table printed in the row order the grid was filled in.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 G-T. 05 R-E OCCURS 2 TIMES. 10 C-E PIC 9(2) OCCURS 2 TIMES. PROCEDURE DIVISION. MOVE 11 TO C-E(1, 1). MOVE 12 TO C-E(1, 2). MOVE 21 TO C-E(2, 1). MOVE 22 TO C-E(2, 2). DISPLAY C-E(2, 1) " " C-E(1, 2). STOP RUN.Answer
21 12
The first subscript picks the row and the second the cell, so the display reads row two cell one and row one cell two.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 G-T. 05 R-E OCCURS 3 TIMES. 10 C-E PIC 9(2) OCCURS 2 TIMES. 01 S-F PIC 9(3) VALUE 0. 01 R-F PIC 9 VALUE 0. 01 C-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM VARYING R-F FROM 1 BY 1 UNTIL R-F IS GREATER THAN 3 PERFORM VARYING C-F FROM 1 BY 1 UNTIL C-F IS GREATER THAN 2 MOVE 1 TO C-E(R-F, C-F) ADD C-E(R-F, C-F) TO S-F END-PERFORM END-PERFORM. DISPLAY S-F. STOP RUN.Answer
006
The six cells are each set to one before they are added, so the total is six.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 G-T. 05 R-E OCCURS 2 TIMES. 10 C-E PIC X(3) OCCURS 2 TIMES. PROCEDURE DIVISION. MOVE "AA" TO C-E(1, 1). MOVE "BB" TO C-E(1, 2). MOVE "CC" TO C-E(2, 1). MOVE "DD" TO C-E(2, 2). DISPLAY "[" C-E(1, 1) "][" C-E(1, 2) "][" C-E(2, 1) "][" C-E(2, 2) "]". STOP RUN.Answer
[AA ][BB ][CC ][DD ]
Each cell is three bytes wide, and the display names them in the order they were filled.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 G-T. 05 R-E OCCURS 2 TIMES. 10 C-E PIC 9(2) OCCURS 3 TIMES. PROCEDURE DIVISION. DISPLAY LENGTH OF G-T. STOP RUN.Answer
12
Two rows of three two-digit cells are twelve bytes.
-
A program reads a file of six fixed cells into a grid of two rows of three. Which cell receives the fourth value?
Answer
Row two, cell one
The fourth value starts the second row, because the first row takes three cells.
-
Why does a grid of a thousand by a hundred ten-byte cells matter to a program that copies it?
Answer
It is a megabyte of working storage, copied in one MOVE
A group move copies every byte, and a million bytes per move is the cost the program has to plan for.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev