In writing the retro-style genealogy app, I was reviewing an older text-book from Internet Archives, “Structured COBOL programming : for the year 2000 and beyond”, by Stern and Stern (9th edition), introduced a new approach to reading and processing a sequential file in ANS85.
The usual approach in ANS74 was as follows;
MAIN. OPEN INPUT SOME-FILE. READ SOME-FILE AT END SET SOME-EOF TO TRUE. PERFORM PROCESS-RECORD UNTIL SOME-EOF. CLOSE SOME-FILE. STOP RUN. PROCESS-RECORD. SOME STATEMENTS. SOME STATEMENTS. ... READ SOME-FILE AT END SET SOME-EOF TO TRUE.
Now, with ANS85, some other commentators change the approach to be (with the addition of PERFORM … END-PERFORM and use of period (or full-stop) on the last statement within the paragraph;
MAIN. OPEN INPUT SOME-FILE READ SOME-FILE AT END SET SOME-EOF TO TRUE PERFORM UNTIL SOME-EOF PERFORM PROCESS-RECORD READ SOME-FILE AT END SET SOME-EOF TO TRUE END-PERFORM CLOSE SOME-FILE STOP RUN. PROCESS-RECORD. SOME STATEMENTS SOME STATEMENTS.
MAIN. OPEN INPUT SOME-FILE READ SOME-FILE AT END SET SOME-EOF TO TRUE PERFORM UNTIL SOME-EOF SOME STATEMENTS SOME STATEMENTS ... READ SOME-FILE AT END SET SOME-EOF TO TRUE END-PERFORM CLOSE SOME-FILE STOP RUN.
The above was the approach I was using, similar with the initial READ prior to the blocked PERFORM … END-PERFORM and a final READ before the END-PERFORM. The approach used by Stern and Stern is interesting with only one READ statement.
MAIN. OPEN INPUT SOME-FILE PERFORM UNTIL SOME-EOF READ SOME-FILE AT END SET SOME-EOF TO TRUE NOT AT END PERFORM PROCESS-RECORD END-READ END-PERFORM CLOSE SOME-FILE STOP RUN. PROCESS-RECORD. SOME STATEMENTS SOME STATEMENTS.
The separate process-record paragraph is not necessary as the few statements could appear instead of ‘PERFORM PROCESS-RECORD’ but a separate paragraph with good naming improves readibility, depending on the number or complexity of the statements used.
... READ SOME-FILE AT END SET SOME-EOF TO TRUE NOT AT END SOME STATEMENT SOME STATEMENT END-READ ...
With the final code (using silly “SOME-” prefixes as opposed to the usual “FD-” and “WS-” prefixes);
000010 IDENTIFICATION DIVISION. 000020 PROGRAM-ID. TEST. 000030 ENVIRONMENT DIVISION. 000040 INPUT-OUTPUT SECTION. 000050 FILE-CONTROL. 000060 SELECT SOME-FILE 000070 ASSIGN TO "./somefile.txt" 000080 ORGANIZATION IS LINE SEQUENTIAL. 000090 DATA DIVISION. 000100 FILE SECTION. 000110 FD SOME-FILE 000120 DATA RECORD IS SOME-RECORD. 000130 01 SOME-RECORD. 000140 88 SOME-EOF VALUE HIGH-VALUES. 000150 02 SOME-STUFF PIC X(128). 000160 PROCEDURE DIVISION. 000170 MAIN. 000180 OPEN INPUT SOME-FILE 000190 PERFORM UNTIL SOME-EOF 000200 READ SOME-FILE 000210 AT END SET 000220 SOME-EOF TO TRUE 000230 NOT AT END 000240 PERFORM PROCESS-RECORD 000250 END-READ 000260 END-PERFORM 000270 CLOSE SOME-FILE 000280 STOP RUN. 000290 PROCESS-RECORD. 000300 DISPLAY SOME-STUFF.
All of which is interesting, possibly a distraction but the book by Stern and Stern makes for some interesting reading.