INITIALIZE and resetting a table

A table lives as long as the program, and a program that fills it twice has the same bytes the second time. A batch job that processes two files in one run, or a menu that starts the same report again, is where a table that was not reset shows entries nobody wrote this time.

What the last pass left

The program below fills three of four entries and prints the table, then fills two entries and prints it again. The entry nobody touched keeps its value from the first pass:

IDENTIFICATION DIVISION. PROGRAM-ID. STALE. DATA DIVISION. WORKING-STORAGE SECTION. 01 NAMES-T. 05 NAME-E OCCURS 4 TIMES. 10 NAME-V PIC X(4). 10 QTY-V PIC 9(3). 01 I-F PIC 9 VALUE 0. PROCEDURE DIVISION. MOVE "AAA" TO NAME-V(1). MOVE 11 TO QTY-V(1). MOVE "BBB" TO NAME-V(2). MOVE 22 TO QTY-V(2). MOVE "CCC" TO NAME-V(3). MOVE 33 TO QTY-V(3). DISPLAY "first pass:". PERFORM SHOW-ALL. MOVE "DDD" TO NAME-V(1). MOVE 44 TO QTY-V(1). MOVE "EEE" TO NAME-V(2). MOVE 55 TO QTY-V(2). DISPLAY "second pass:". PERFORM SHOW-ALL. STOP RUN. SHOW-ALL. PERFORM VARYING I-F FROM 1 BY 1 UNTIL I-F > 4 DISPLAY I-F " [" NAME-V(I-F) "] " QTY-V(I-F) END-PERFORM.

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

first pass:
1 [AAA ] 011
2 [BBB ] 022
3 [CCC ] 033
4 [ ] 000
second pass:
1 [DDD ] 044
2 [EEE ] 055
3 [CCC ] 033
4 [ ] 000

The third line of the second pass is the bug: the entry was not written this time and the old value is still there, so a report that prints the whole table reports a name from the previous file. The loop that fills the table knows how many entries it wrote, and the loop that prints it does not; a count field would cover this case and not the next one, where the second pass writes fewer entries into a table of a different layout.

INITIALIZE the group

INITIALIZE resets a group to the values a fresh run would have: spaces in the alphanumeric fields and zeros in the numeric ones. It applies to every entry of a table in one statement:

IDENTIFICATION DIVISION. PROGRAM-ID. INIT. DATA DIVISION. WORKING-STORAGE SECTION. 01 NAMES-T. 05 NAME-E OCCURS 3 TIMES. 10 NAME-V PIC X(4). 10 QTY-V PIC 9(3). 01 I-F PIC 9 VALUE 0. PROCEDURE DIVISION. MOVE "AAA" TO NAME-V(1). MOVE "BBB" TO NAME-V(2). MOVE "CCC" TO NAME-V(3). MOVE 11 TO QTY-V(1). MOVE 22 TO QTY-V(2). MOVE 33 TO QTY-V(3). DISPLAY "before:". PERFORM SHOW-ALL. INITIALIZE NAMES-T. DISPLAY "after INITIALIZE:". PERFORM SHOW-ALL. INITIALIZE NAMES-T REPLACING NUMERIC BY 9. DISPLAY "after REPLACING NUMERIC BY 9:". PERFORM SHOW-ALL. STOP RUN. SHOW-ALL. PERFORM VARYING I-F FROM 1 BY 1 UNTIL I-F > 3 DISPLAY I-F " [" NAME-V(I-F) "] " QTY-V(I-F) END-PERFORM.

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

before:
1 [AAA ] 011
2 [BBB ] 022
3 [CCC ] 033
after INITIALIZE:
1 [ ] 000
2 [ ] 000
3 [ ] 000
after REPLACING NUMERIC BY 9:
1 [ ] 009
2 [ ] 009
3 [ ] 009

INITIALIZE REPLACING

The REPLACING clause changes what a fresh value is for one category of field. NUMERIC BY 9 fills the quantities with nines, which is the classic way to say "no maximum yet" for a field that later takes the smallest value it sees.

The categories are the same ones the IF uses: NUMERIC, ALPHANUMERIC, ALPHABETIC. A field of a group with a REDEFINES or an OCCURS DEPENDING ON is a case where the statement's reach is worth checking in the manual rather than assuming; for a plain table it resets exactly the bytes the program declared.

LOCAL-STORAGE, the other answer

A table in WORKING-STORAGE keeps its values for the life of the run, which is what the first program shows. A table in LOCAL-STORAGE is created fresh on every call of the program that owns it, so a subprogram called twice sees zeros and spaces the second time without an INITIALIZE.

That section is not defined in this compiler for a nested program, so the runnable demonstration belongs where a subprogram is compiled on its own: CALL and USING and recursion. The rule for this lesson is the simple one: a table that a program fills more than once is initialized at the start of every fill, or the second fill inherits the first.

Exercises

  1. Add INITIALIZE NAMES-T before the second pass of the first program and run it again. Which lines change?
  2. Reset only the quantities with INITIALIZE ... REPLACING NUMERIC BY 0. What happens to the names?
  3. Reset the table with a loop that moves SPACES and ZERO into each entry. How many statements does the loop need against the one INITIALIZE?
  4. Keep a count of the entries the second pass writes and print only those. Which is the better fix for the report, the count or the INITIALIZE?

Questions

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

  1. Why does the second pass of the first program print a name from the first?

    Answer

    The table kept its bytes, and the entry was not written this time

    The third entry still held CCC and 033, because nothing reset it before the second fill.

  2. What does INITIALIZE do to a group?

    Answer

    It sets alphanumeric fields to spaces and numeric fields to zeros

    The run printed spaces and zeros for every field of the table.

  3. What does INITIALIZE ... REPLACING NUMERIC BY 9 change?

    Answer

    The numeric fields get nines instead of zeros

    The quantities printed 009 after the statement and the names stayed spaces.

  4. When does LOCAL-STORAGE get its initial values?

    Answer

    On every call of the program that declares it

    That is the difference from WORKING-STORAGE, which keeps its values for the whole run.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 T-T. 05 E-E OCCURS 2 TIMES. 10 E-V PIC X(2). 10 E-N PIC 9(2). PROCEDURE DIVISION. MOVE "AA" TO E-V(1). MOVE 11 TO E-N(1). INITIALIZE T-T. DISPLAY "[" E-V(1) "][" E-N(1) "]". STOP RUN.

    Answer

    [ ][00]

    The alphanumeric field is reset to spaces and the numeric one to zeros.

  6. 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-V PIC X(2). 10 E-N PIC 9(2). PROCEDURE DIVISION. MOVE 42 TO E-N(1). INITIALIZE T-T REPLACING NUMERIC BY 9. DISPLAY E-N(1) " " E-N(2). STOP RUN.

    Answer

    09 09

    Every numeric field of both entries takes the replacing value, printed in two digits.

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

    Answer

    000

    The reset clears all three entries before the loop adds them.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 T-T. 05 E-E OCCURS 2 TIMES. 10 E-V PIC X(3). PROCEDURE DIVISION. MOVE "ABC" TO E-V(1). MOVE "DEF" TO E-V(2). MOVE SPACES TO E-V(1). DISPLAY "[" E-V(1) "][" E-V(2) "]". STOP RUN.

    Answer

    [ ][DEF]

    The MOVE clears only the first entry, and the second keeps its value.

  9. A batch program runs the same report paragraph twice in one job and the second report lists rows from the first file. What is missing?

    Answer

    An INITIALIZE of the table at the start of the fill

    The table keeps its bytes between reports, so the second fill has to reset what it does not write.

  10. Why is INITIALIZE better than a loop that moves SPACES and ZERO?

    Answer

    It covers every field of the group, including ones added later

    A hand-written loop names the fields it knows; the statement takes the group as declared.

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