OCCURS DEPENDING ON

A file arrives with three entries today and five tomorrow. A table of five wastes two entries and a program that loops to five reads them; a table with OCCURS DEPENDING ON has a maximum and a count, and the count is an ordinary field the program sets.

OCCURS DEPENDING ON

The clause names the lowest and the highest number of entries, and a field that says how many are in use. The field is declared before the table, and the table lives in the maximum number of entries either way:

IDENTIFICATION DIVISION. PROGRAM-ID. ODO. DATA DIVISION. WORKING-STORAGE SECTION. 01 USED-N PIC 9 VALUE 0. 01 LIST-T. 05 NAME-E PIC X(6) OCCURS 1 TO 5 TIMES DEPENDING ON USED-N. 01 I-F PIC 9 VALUE 0. PROCEDURE DIVISION. MOVE "Ada" TO NAME-E(1). MOVE "Grace" TO NAME-E(2). MOVE "Alan" TO NAME-E(3). MOVE 3 TO USED-N. DISPLAY "used " USED-N. PERFORM VARYING I-F FROM 1 BY 1 UNTIL I-F > USED-N DISPLAY "name " I-F " [" NAME-E(I-F) "]" END-PERFORM. DISPLAY "length " LENGTH OF LIST-T. MOVE 5 TO USED-N. DISPLAY "length at five " LENGTH OF LIST-T. STOP RUN.

cobc -x -free -Wall -o odo odo.cob
./odo

used 3
name 1 [Ada ]
name 2 [Grace ]
name 3 [Alan ]
length +000000018
length at five +000000030

The loop bound is the count

The loop ends at USED-N and not at five, so the two entries that hold nothing are not printed. The entries themselves were filled before the count was set, which is legal: the count decides how much of the table the program uses, not how much of it exists.

That is also the danger. A read that sets the count after the loop, or a count that one paragraph sets and another forgets, gives a program that reads the wrong number of entries with no error. The count is a field like any other and the compiler does not tie it to the loop.

LENGTH OF follows the count

LENGTH OF LIST-T is 18 while the count is three and 30 when it is five: the compiler reads the count field at run time and returns the bytes in use, not the bytes reserved. The sign in front of the number is part of the result of the function, which is a signed integer.

This is what lets a program write a record whose length is the number of entries it filled. A file record that ends with such a table uses RECORD VARYING IN SIZE DEPENDING ON in its FD, which the lesson on SELECT and FD shows in place; this page keeps to the table itself.

A count above the maximum

The count field is a PIC 9 and can hold a value the table cannot: six is above the maximum of five. A loop that trusts it reads an entry past the end of the table. The plain build reported nothing; the checked build stopped with the message:

IDENTIFICATION DIVISION. PROGRAM-ID. OVER. DATA DIVISION. WORKING-STORAGE SECTION. 01 USED-N PIC 9 VALUE 6. 01 LIST-T. 05 NAME-E PIC X(6) OCCURS 1 TO 5 TIMES DEPENDING ON USED-N. 01 I-F PIC 9 VALUE 0. PROCEDURE DIVISION. MOVE "Ada" TO NAME-E(1). MOVE 6 TO USED-N. DISPLAY "used " USED-N. PERFORM VARYING I-F FROM 1 BY 1 UNTIL I-F > USED-N DISPLAY "name " I-F " [" NAME-E(I-F) "]" END-PERFORM. STOP RUN.

cobc -x -free -Wall -debug -o overd over.cob
./overd
echo "exit status $?"

libcob: over.cob:12: error: OCCURS DEPENDING ON 'USED-N' out of bounds: 6
note: maximum subscript for 'NAME-E': 5

Last statement of "OVER" was MOVE at line 12 of over.cob
ENTRY OVER at over.cob:12
Started by ./overd
exit status 1

The statement that failed is the MOVE 6 TO USED-N, because that is where the count became impossible. The check belongs to the same -debug build as the subscript check of the previous lesson, and it is the only thing in this image that notices a count above the maximum.

The next lesson gives the table an index instead of a subscript: an index is a name the compiler keeps, moved with SET, and it is what SEARCH uses.

Exercises

  1. Set USED-N to 0 and run the first program. What do the two length lines print?
  2. Move USED-N after the table instead of before it and build. What does the compiler say?
  3. Change the maximum to 10 and print the length at three and at ten. What is the difference per entry?
  4. Write a paragraph that rejects a count above the maximum before the loop and prints the count it refused.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does OCCURS 1 TO 5 TIMES DEPENDING ON USED-N mean?

    Answer

    The table has at most five entries and USED-N says how many are in use

    The maximum is fixed at compile time and the count field selects how much of it the program uses.

  2. What does LENGTH OF return for the table when the count is three?

    Answer

    The bytes in use: eighteen

    The run printed 18 at a count of three and 30 at a count of five, so the function follows the count.

  3. Where does the count field have to be declared?

    Answer

    Before the table that depends on it

    The table clause names the field, so the field has to be defined before it.

  4. What happens when the count is above the maximum?

    Answer

    The plain build reports nothing and a -debug build stops on the statement that set the count

    The checked run reported the count out of bounds at the MOVE that set it.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 2. 01 T-T. 05 E-F PIC X(3) OCCURS 1 TO 4 TIMES DEPENDING ON N-F. 01 I-F PIC 9 VALUE 0. PROCEDURE DIVISION. MOVE "AA" TO E-F(1). MOVE "BB" TO E-F(2). MOVE "CC" TO E-F(3). PERFORM VARYING I-F FROM 1 BY 1 UNTIL I-F IS GREATER THAN N-F DISPLAY "[" E-F(I-F) "]" END-PERFORM. STOP RUN.

    Answer

    [AA ] / [BB ]

    The count is two, so the loop prints two entries although a third was written.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 4. 01 T-T. 05 E-F PIC 9(2) OCCURS 1 TO 4 TIMES DEPENDING ON N-F. PROCEDURE DIVISION. DISPLAY LENGTH OF T-T. STOP RUN.

    Answer

    +000000008

    Four entries of two bytes are eight bytes, and the length is printed as a signed value.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 1. 01 T-T. 05 E-F PIC 9(2) OCCURS 1 TO 3 TIMES DEPENDING ON N-F. 01 I-F PIC 9 VALUE 0. 01 S-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. MOVE 5 TO E-F(1). MOVE 6 TO E-F(2). MOVE 7 TO E-F(3). PERFORM VARYING I-F FROM 1 BY 1 UNTIL I-F IS GREATER THAN N-F ADD E-F(I-F) TO S-F END-PERFORM. DISPLAY S-F. STOP RUN.

    Answer

    005

    A count of one makes the loop add only the first entry.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 3. 01 T-T. 05 E-F PIC X(4) OCCURS 1 TO 5 TIMES DEPENDING ON N-F. 01 I-F PIC 9 VALUE 0. PROCEDURE DIVISION. MOVE "X" TO E-F(1). MOVE "Y" TO E-F(2). MOVE "Z" TO E-F(3). PERFORM VARYING I-F FROM 3 BY -1 UNTIL I-F IS LESS THAN 1 DISPLAY "[" E-F(I-F) "]" END-PERFORM. STOP RUN.

    Answer

    [Z ] / [Y ] / [X ]

    The loop counts down from three, so the entries print in reverse order and each is four bytes wide.

  9. A program sets the count after its loop has already run. What does the user see?

    Answer

    Entries the program meant to print are missing, with no error

    The count is only a field, so a loop that reads it before it is set uses the old value silently.

  10. What is the difference between an OCCURS table and an OCCURS DEPENDING ON table?

    Answer

    Both reserve the maximum, and the ODO table also has a count that says how much is in use

    The storage is the maximum in both cases; the count and LENGTH OF decide how much of it is used.

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