Tables of group items
A table of numbers is a convenience. A table of records is a design: it holds the rows a program loaded from a file, and every field of a row is reachable with the same subscript. In COBOL that is one OCCURS on the group, and the fields declared under it.
OCCURS on a group
The OCCURS goes on the level that holds the record, and the fields of the record sit under it without a clause of their own:
IDENTIFICATION DIVISION.
PROGRAM-ID. RECS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ORDERS-T.
05 ORDER-E OCCURS 4 TIMES.
10 ORDER-ID PIC X(3).
10 ORDER-QTY PIC 9(3).
10 ORDER-AMT PIC 9(5).
01 OUT-REC.
05 OUT-ID PIC X(3).
05 OUT-QTY PIC 9(3).
05 OUT-AMT PIC 9(5).
01 I-F PIC 9 VALUE 0.
01 TOTAL-F PIC 9(7) VALUE 0.
PROCEDURE DIVISION.
MOVE "A10" TO ORDER-ID(1).
MOVE 002 TO ORDER-QTY(1).
MOVE 01500 TO ORDER-AMT(1).
MOVE "B20" TO ORDER-ID(2).
MOVE 005 TO ORDER-QTY(2).
MOVE 00750 TO ORDER-AMT(2).
MOVE "C30" TO ORDER-ID(3).
MOVE 001 TO ORDER-QTY(3).
MOVE 00099 TO ORDER-AMT(3).
MOVE "D40" TO ORDER-ID(4).
MOVE 003 TO ORDER-QTY(4).
MOVE 00450 TO ORDER-AMT(4).
PERFORM VARYING I-F FROM 1 BY 1 UNTIL I-F > 4
DISPLAY ORDER-ID(I-F) " qty " ORDER-QTY(I-F)
" amt " ORDER-AMT(I-F)
ADD ORDER-AMT(I-F) TO TOTAL-F
END-PERFORM.
MOVE ORDER-E(2) TO OUT-REC.
DISPLAY "second order " OUT-ID " qty " OUT-QTY.
DISPLAY "total " TOTAL-F.
DISPLAY "entry bytes " LENGTH OF ORDER-E(1).
STOP RUN.
cobc -x -free -Wall -o recs recs.cob
./recs
A10 qty 002 amt 01500
B20 qty 005 amt 00750
C30 qty 001 amt 00099
D40 qty 003 amt 00450
second order B20 qty 005
total 0002799
entry bytes 11
A field of one entry
The subscript goes after the field name and not after the group: ORDER-AMT(I-F) is the amount of one order, and ORDER-E(2) is the second order as a whole. There is no separate array type: the compiler knows, from the declarations, that ORDER-AMT repeats four times inside ORDER-E, and the subscript picks one copy.
That is also why a field of a table cannot be used without a subscript unless the table has one entry, and why a MOVE that forgets the subscript is refused rather than silently reading the first entry.
One entry moved into a record
MOVE ORDER-E(2) TO OUT-REC copies eleven bytes: the group and the record have the same layout, and the move is by position, so the fields land where the record declares them. The display after it shows the second order's identifier and quantity in the record fields.
This is how a program hands one row of a table to a report paragraph, to a subprogram or to a file write: the row becomes a record with a name of its own, and the paragraph that prints it does not need the table or the subscript. The same move with a record of a different layout is legal and wrong, because the compiler compares positions and not meanings.
Searching a table of records
With an index on the group, SEARCH finds a record by one of its fields:
IDENTIFICATION DIVISION.
PROGRAM-ID. FINDONE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ORDERS-T.
05 ORDER-E OCCURS 4 TIMES INDEXED BY OI.
10 ORDER-ID PIC X(3).
10 ORDER-QTY PIC 9(3).
01 SEEK-F PIC X(3) VALUE "C30".
PROCEDURE DIVISION.
MOVE "A10" TO ORDER-ID(1).
MOVE 2 TO ORDER-QTY(1).
MOVE "B20" TO ORDER-ID(2).
MOVE 5 TO ORDER-QTY(2).
MOVE "C30" TO ORDER-ID(3).
MOVE 1 TO ORDER-QTY(3).
MOVE "D40" TO ORDER-ID(4).
MOVE 3 TO ORDER-QTY(4).
SET OI TO 1.
SEARCH ORDER-E
AT END DISPLAY "order " SEEK-F " not found"
WHEN ORDER-ID(OI) = SEEK-F
DISPLAY "order " ORDER-ID(OI) " qty " ORDER-QTY(OI)
END-SEARCH.
STOP RUN.
cobc -x -free -Wall -o findone find.cob
./findone
order C30 qty 001
The WHEN compares one field of the entry, and the branch reads other fields of the same entry with the same index. That is the whole of a lookup in working storage, and the next lesson is the question this program does not answer: what a table holds when the program has run before.
Exercises
- Add a field for the region to each order and total the amounts by region with three counters.
- Move ORDER-E(3) into the record and print it. Which fields of the record change?
- Print an entry without a subscript. What does the compiler say?
- Change the search key to ORDER-QTY(OI) = 3 and run it. Which order is found, and why is the key a bad one?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where does OCCURS go in a table of records?
Answer
On the group that holds the record, with the fields under it
One clause on the record level repeats the whole record and every field inside it.
-
What does ORDER-AMT(3) name?
Answer
The amount field of the third order
The subscript picks one copy of the repeating field inside the repeating record.
-
What does MOVE ORDER-E(2) TO OUT-REC do?
Answer
It copies the bytes of the second order into the record by position
A group move copies every byte in order, which is why the layouts have to match.
-
What does LENGTH OF ORDER-E(1) return for the record of this page?
Answer
Eleven bytes: three for the id, three for the quantity, five for the amount
The program printed 11, the sum of the field widths of one record.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 T-T. 05 E-E OCCURS 3 TIMES. 10 E-ID PIC X(2). 10 E-N PIC 9(2). PROCEDURE DIVISION. MOVE "AA" TO E-ID(1). MOVE 10 TO E-N(1). MOVE "BB" TO E-ID(2). MOVE 20 TO E-N(2). MOVE "CC" TO E-ID(3). MOVE 30 TO E-N(3). DISPLAY E-ID(2) " " E-N(2). STOP RUN.Answer
BB 20
Both fields take subscript two, which is the second record.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 T-T. 05 E-E OCCURS 2 TIMES. 10 E-ID PIC X(2). 10 E-N PIC 9(2). 01 S-F PIC 9(3) VALUE 0. 01 I-F PIC 9 VALUE 0. PROCEDURE DIVISION. MOVE 10 TO E-N(1). MOVE 15 TO E-N(2). PERFORM VARYING I-F FROM 1 BY 1 UNTIL I-F IS GREATER THAN 2 ADD E-N(I-F) TO S-F END-PERFORM. DISPLAY S-F. STOP RUN.Answer
025
The loop adds the quantity field of both records, ten and fifteen.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 T-T. 05 E-E OCCURS 2 TIMES. 10 E-ID PIC X(2). 10 E-N PIC 9(2). 01 R-R. 05 R-ID PIC X(2). 05 R-N PIC 9(2). PROCEDURE DIVISION. MOVE "AA" TO E-ID(1). MOVE 10 TO E-N(1). MOVE "BB" TO E-ID(2). MOVE 20 TO E-N(2). MOVE E-E(2) TO R-R. DISPLAY "[" R-ID "] " R-N. STOP RUN.Answer
[BB] 20
The group move copies both fields of the second record into the record with the same layout.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 T-T. 05 E-E OCCURS 3 TIMES INDEXED BY I. 10 E-ID PIC X(2). 01 K-F PIC X(2) VALUE "CC". PROCEDURE DIVISION. MOVE "AA" TO E-ID(1). MOVE "BB" TO E-ID(2). MOVE "CC" TO E-ID(3). SET I TO 1. SEARCH E-E AT END DISPLAY "none" WHEN E-ID(I) = K-F DISPLAY "found " I END-SEARCH. STOP RUN.Answer
found +000000003
The search compares the identifier field of each record and stops on the third.
-
A program has a table of four records and reads a fifth into it with a loop that runs five times. What breaks?
Answer
The fifth write goes past the table, which only -debug reports
The table is fixed at four records and no check runs without the debug build, as the lesson on OCCURS showed.
-
Why is a table of records the natural shape for a file read into memory?
Answer
Each record already has the layout of the file record, and one subscript selects a row
One entry is the record of the file, so a read fills one entry and a search finds one row.
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