SORT with procedures
USING and GIVING are the short form: hand the sort a file, take a file back. A sort that has to change the records on the way through replaces the two with procedures: INPUT PROCEDURE reads and releases what it wants, OUTPUT PROCEDURE returns records one at a time and writes whatever the report needs.
INPUT PROCEDURE
With an input procedure the program opens the input file itself, reads it, and hands the records it wants to the sort with RELEASE. Records it does not release never reach the sort:
IDENTIFICATION DIVISION.
PROGRAM-ID. INPROC.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-F ASSIGN TO "raw.dat"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-IN-ST.
SELECT SORT-F ASSIGN TO "sortw.dat"
FILE STATUS IS WS-SORT-ST.
SELECT OUT-F ASSIGN TO "kept.dat"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-OUT-ST.
DATA DIVISION.
FILE SECTION.
FD IN-F.
01 IN-REC.
05 IN-NAME PIC X(8).
05 IN-AMOUNT PIC 9(5).
SD SORT-F.
01 SORT-REC.
05 S-NAME PIC X(8).
05 S-AMOUNT PIC 9(5).
FD OUT-F.
01 OUT-REC.
05 OUT-NAME PIC X(8).
05 OUT-AMOUNT PIC 9(5).
WORKING-STORAGE SECTION.
01 WS-IN-ST PIC XX.
01 WS-SORT-ST PIC XX.
01 WS-OUT-ST PIC XX.
01 WS-EOF PIC X VALUE "N".
01 WS-RELEASED PIC 9(3) VALUE 0.
01 WS-DROPPED PIC 9(3) VALUE 0.
PROCEDURE DIVISION.
OPEN OUTPUT IN-F.
MOVE "pear" TO IN-NAME.
MOVE 300 TO IN-AMOUNT.
WRITE IN-REC.
MOVE "fig" TO IN-NAME.
MOVE 40 TO IN-AMOUNT.
WRITE IN-REC.
MOVE "apple" TO IN-NAME.
MOVE 250 TO IN-AMOUNT.
WRITE IN-REC.
MOVE "date" TO IN-NAME.
MOVE 75 TO IN-AMOUNT.
WRITE IN-REC.
CLOSE IN-F.
OPEN INPUT IN-F.
SORT SORT-F
ON ASCENDING KEY S-NAME
INPUT PROCEDURE IS FILTER-IN
GIVING OUT-F.
DISPLAY "sort " WS-SORT-ST.
DISPLAY "released " WS-RELEASED " dropped " WS-DROPPED.
OPEN INPUT OUT-F.
PERFORM UNTIL WS-OUT-ST = "10"
READ OUT-F
AT END MOVE "10" TO WS-OUT-ST
NOT AT END DISPLAY OUT-NAME " " OUT-AMOUNT
END-READ
END-PERFORM.
CLOSE OUT-F.
STOP RUN.
FILTER-IN SECTION.
PERFORM UNTIL WS-EOF = "Y"
READ IN-F
AT END MOVE "Y" TO WS-EOF
NOT AT END
IF IN-AMOUNT >= 100
MOVE IN-REC TO SORT-REC
RELEASE SORT-REC
ADD 1 TO WS-RELEASED
ELSE
ADD 1 TO WS-DROPPED
END-IF
END-READ
END-PERFORM.
CLOSE IN-F.
cobc -x -free -Wall -o inproc inproc.cob
./inproc
sort 00
released 002 dropped 002
apple 00250
pear 00300
Four records went in, two were released and two dropped, and the output file holds the two survivors in name order. The SORT statement still has a GIVING: the output side of this sort is a plain file, only the input side needed code.
The procedure is a SECTION, not a paragraph, and the program closes the input file inside it. A section that leaves the file open leaks a descriptor, and a job that runs a thousand sorts in one step runs out of them.
OUTPUT PROCEDURE
The output procedure takes the place of GIVING. It calls RETURN in a loop, and each call hands it the next record in sorted order:
IDENTIFICATION DIVISION.
PROGRAM-ID. OUTPROC.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-F ASSIGN TO "rows.dat"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-IN-ST.
SELECT SORT-F ASSIGN TO "sortw2.dat"
FILE STATUS IS WS-SORT-ST.
SELECT REP-F ASSIGN TO "report.txt"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-REP-ST.
DATA DIVISION.
FILE SECTION.
FD IN-F.
01 IN-REC.
05 IN-NAME PIC X(8).
05 IN-AMOUNT PIC 9(5).
SD SORT-F.
01 SORT-REC.
05 S-NAME PIC X(8).
05 S-AMOUNT PIC 9(5).
FD REP-F.
01 REP-REC PIC X(40).
WORKING-STORAGE SECTION.
01 WS-IN-ST PIC XX.
01 WS-SORT-ST PIC XX.
01 WS-REP-ST PIC XX.
01 WS-RETURNED PIC 9(3) VALUE 0.
01 WS-LINE PIC X(40).
PROCEDURE DIVISION.
OPEN OUTPUT IN-F.
MOVE "pear" TO IN-NAME.
MOVE 300 TO IN-AMOUNT.
WRITE IN-REC.
MOVE "fig" TO IN-NAME.
MOVE 40 TO IN-AMOUNT.
WRITE IN-REC.
MOVE "apple" TO IN-NAME.
MOVE 25 TO IN-AMOUNT.
WRITE IN-REC.
CLOSE IN-F.
OPEN OUTPUT REP-F.
SORT SORT-F
ON ASCENDING KEY S-NAME
USING IN-F
OUTPUT PROCEDURE IS REPORT-OUT.
DISPLAY "sort " WS-SORT-ST.
DISPLAY "returned " WS-RETURNED.
CLOSE REP-F.
OPEN INPUT REP-F.
PERFORM UNTIL WS-REP-ST = "10"
READ REP-F
AT END MOVE "10" TO WS-REP-ST
NOT AT END DISPLAY "[" FUNCTION TRIM(REP-REC) "]"
END-READ
END-PERFORM.
CLOSE REP-F.
STOP RUN.
REPORT-OUT SECTION.
PERFORM UNTIL WS-SORT-ST = "10"
RETURN SORT-F
AT END MOVE "10" TO WS-SORT-ST
NOT AT END
ADD 1 TO WS-RETURNED
MOVE SPACES TO WS-LINE
STRING S-NAME DELIMITED BY SPACE
" " DELIMITED BY SIZE
S-AMOUNT DELIMITED BY SIZE
INTO WS-LINE
END-STRING
MOVE WS-LINE TO REP-REC
WRITE REP-REC
END-RETURN
END-PERFORM.
cobc -x -free -Wall -o outproc outproc.cob
./outproc
sort 00
returned 003
[apple 00025]
[fig 00040]
[pear 00300]
The MOVE SPACES TO WS-LINE is not housekeeping. A first version of this program without it printed [fig 0004025] for the second record: the STRING had written fig and 00040 over the old line and left the last two characters of apple behind. The report looked plausible and the amounts were wrong.
Note also what the file holds: eleven characters per line, not forty. A line sequential file drops the trailing spaces of a record, so the print file is as long as its text and the reader of the report sees no padding.
Both procedures
One sort can take both: the input procedure filters, the output procedure formats, and the sort writes no middle file at all:
IDENTIFICATION DIVISION.
PROGRAM-ID. BOTH.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-F ASSIGN TO "raw2.dat"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-IN-ST.
SELECT SORT-F ASSIGN TO "sortw3.dat"
FILE STATUS IS WS-SORT-ST.
SELECT REP-F ASSIGN TO "report2.txt"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-REP-ST.
DATA DIVISION.
FILE SECTION.
FD IN-F.
01 IN-REC.
05 IN-NAME PIC X(8).
05 IN-AMOUNT PIC 9(5).
SD SORT-F.
01 SORT-REC.
05 S-NAME PIC X(8).
05 S-AMOUNT PIC 9(5).
FD REP-F.
01 REP-REC PIC X(40).
WORKING-STORAGE SECTION.
01 WS-IN-ST PIC XX.
01 WS-SORT-ST PIC XX.
01 WS-REP-ST PIC XX.
01 WS-EOF PIC X VALUE "N".
01 WS-RELEASED PIC 9(3) VALUE 0.
01 WS-DROPPED PIC 9(3) VALUE 0.
01 WS-RETURNED PIC 9(3) VALUE 0.
01 WS-TOTAL PIC 9(7) VALUE 0.
01 WS-LINE PIC X(40).
PROCEDURE DIVISION.
OPEN OUTPUT IN-F.
MOVE "pear" TO IN-NAME.
MOVE 300 TO IN-AMOUNT.
WRITE IN-REC.
MOVE "fig" TO IN-NAME.
MOVE 40 TO IN-AMOUNT.
WRITE IN-REC.
MOVE "apple" TO IN-NAME.
MOVE 250 TO IN-AMOUNT.
WRITE IN-REC.
MOVE "date" TO IN-NAME.
MOVE 75 TO IN-AMOUNT.
WRITE IN-REC.
CLOSE IN-F.
OPEN INPUT IN-F.
OPEN OUTPUT REP-F.
SORT SORT-F
ON ASCENDING KEY S-NAME
INPUT PROCEDURE IS FILTER-IN
OUTPUT PROCEDURE IS REPORT-OUT.
DISPLAY "sort " WS-SORT-ST.
DISPLAY "released " WS-RELEASED " dropped " WS-DROPPED.
DISPLAY "returned " WS-RETURNED " total " WS-TOTAL.
CLOSE REP-F.
OPEN INPUT REP-F.
PERFORM UNTIL WS-REP-ST = "10"
READ REP-F
AT END MOVE "10" TO WS-REP-ST
NOT AT END DISPLAY "[" FUNCTION TRIM(REP-REC) "]"
END-READ
END-PERFORM.
CLOSE REP-F.
STOP RUN.
FILTER-IN SECTION.
PERFORM UNTIL WS-EOF = "Y"
READ IN-F
AT END MOVE "Y" TO WS-EOF
NOT AT END
IF IN-AMOUNT >= 100
MOVE IN-REC TO SORT-REC
RELEASE SORT-REC
ADD 1 TO WS-RELEASED
ELSE
ADD 1 TO WS-DROPPED
END-IF
END-READ
END-PERFORM.
CLOSE IN-F.
REPORT-OUT SECTION.
PERFORM UNTIL WS-SORT-ST = "10"
RETURN SORT-F
AT END MOVE "10" TO WS-SORT-ST
NOT AT END
ADD 1 TO WS-RETURNED
ADD S-AMOUNT TO WS-TOTAL
MOVE SPACES TO WS-LINE
STRING S-NAME DELIMITED BY SPACE
" " DELIMITED BY SIZE
S-AMOUNT DELIMITED BY SIZE
INTO WS-LINE
END-STRING
MOVE WS-LINE TO REP-REC
WRITE REP-REC
END-RETURN
END-PERFORM.
cobc -x -free -Wall -o both both.cob
./both
sort 00
released 002 dropped 002
returned 002 total 0000550
[apple 00250]
[pear 00300]
The three counters are the audit trail of the step: four records read, two released, two dropped, two returned, and 250 + 300 is the 550 the output procedure accumulated. A batch job that logs those five numbers can be checked against its input without reading a single record.
A sort of no records
The empty input is the case that breaks hand-written sort logic, and the verb handles it: the input procedure reads nothing, the output procedure returns nothing on its first call, and the report file is created empty:
IDENTIFICATION DIVISION.
PROGRAM-ID. NOBODY.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-F ASSIGN TO "raw2.dat"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-IN-ST.
SELECT SORT-F ASSIGN TO "sortw3.dat"
FILE STATUS IS WS-SORT-ST.
SELECT REP-F ASSIGN TO "report2.txt"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-REP-ST.
DATA DIVISION.
FILE SECTION.
FD IN-F.
01 IN-REC.
05 IN-NAME PIC X(8).
05 IN-AMOUNT PIC 9(5).
SD SORT-F.
01 SORT-REC.
05 S-NAME PIC X(8).
05 S-AMOUNT PIC 9(5).
FD REP-F.
01 REP-REC PIC X(40).
WORKING-STORAGE SECTION.
01 WS-IN-ST PIC XX.
01 WS-SORT-ST PIC XX.
01 WS-REP-ST PIC XX.
01 WS-EOF PIC X VALUE "N".
01 WS-RELEASED PIC 9(3) VALUE 0.
01 WS-DROPPED PIC 9(3) VALUE 0.
01 WS-RETURNED PIC 9(3) VALUE 0.
01 WS-TOTAL PIC 9(7) VALUE 0.
01 WS-LINE PIC X(40).
PROCEDURE DIVISION.
OPEN OUTPUT IN-F.
CLOSE IN-F.
OPEN INPUT IN-F.
OPEN OUTPUT REP-F.
SORT SORT-F
ON ASCENDING KEY S-NAME
INPUT PROCEDURE IS FILTER-IN
OUTPUT PROCEDURE IS REPORT-OUT.
DISPLAY "sort " WS-SORT-ST.
DISPLAY "released " WS-RELEASED " dropped " WS-DROPPED.
DISPLAY "returned " WS-RETURNED " total " WS-TOTAL.
CLOSE REP-F.
OPEN INPUT REP-F.
PERFORM UNTIL WS-REP-ST = "10"
READ REP-F
AT END MOVE "10" TO WS-REP-ST
NOT AT END DISPLAY "[" FUNCTION TRIM(REP-REC) "]"
END-READ
END-PERFORM.
DISPLAY "report status " WS-REP-ST " lines printed above".
CLOSE REP-F.
STOP RUN.
FILTER-IN SECTION.
PERFORM UNTIL WS-EOF = "Y"
READ IN-F
AT END MOVE "Y" TO WS-EOF
NOT AT END
IF IN-AMOUNT >= 100
MOVE IN-REC TO SORT-REC
RELEASE SORT-REC
ADD 1 TO WS-RELEASED
ELSE
ADD 1 TO WS-DROPPED
END-IF
END-READ
END-PERFORM.
CLOSE IN-F.
REPORT-OUT SECTION.
PERFORM UNTIL WS-SORT-ST = "10"
RETURN SORT-F
AT END MOVE "10" TO WS-SORT-ST
NOT AT END
ADD 1 TO WS-RETURNED
ADD S-AMOUNT TO WS-TOTAL
MOVE SPACES TO WS-LINE
STRING S-NAME DELIMITED BY SPACE
" " DELIMITED BY SIZE
S-AMOUNT DELIMITED BY SIZE
INTO WS-LINE
END-STRING
MOVE WS-LINE TO REP-REC
WRITE REP-REC
END-RETURN
END-PERFORM.
cobc -x -free -Wall -o nobody nobody.cob
./nobody
ls -l report2.txt
sort 00
released 000 dropped 000
returned 000 total 0000000
report status 10 lines printed above
-rw-r--r-- 1 agent agent 0 Nov 18 09:48 report2.txt
Every counter is zero, the status is 00, and the report is a file of no bytes. That is the right answer for a night with no input: the report exists, the step succeeded, and the counters prove that nothing was silently lost.
Exercises
- Change the filter of inproc to drop names that start with a letter above M. Which records survive?
- Remove MOVE SPACES TO WS-LINE from outproc and read the second line of the report.
- Add a page number to every line of the report of both.
- Run nobody with the input file of both in place and see the counters change.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which verb hands a record to a sort from an INPUT PROCEDURE?
Answer
RELEASE
The filter paragraph moved the record into the SD area and released it.
-
Which verb takes a record out of a sort in an OUTPUT PROCEDURE?
Answer
RETURN
The report paragraph called RETURN in a loop until the sort was empty.
-
What does SORT do with the records an input procedure does not release?
Answer
It never sees them, so they are not in the output
Two of the four records were dropped and the output held two.
-
What does a line sequential file do with the trailing spaces of a record?
Answer
It drops them, so the line is as long as its text
The report file held eleven bytes per line for a PIC X(40) record.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-LINE PIC X(10) VALUE "0123456789". PROCEDURE DIVISION. MOVE SPACES TO W-LINE. STRING "ab" DELIMITED BY SIZE INTO W-LINE END-STRING. DISPLAY "[" W-LINE "]". STOP RUN.Answer
[ab ]
The line was cleared first, so the two characters sit in a field of spaces.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-L PIC X(6) VALUE "abcdef". PROCEDURE DIVISION. STRING "xy" DELIMITED BY SIZE INTO W-L END-STRING. DISPLAY W-L. STOP RUN.Answer
xycdef
With no POINTER and no clear, the two characters overwrite the first two and the rest stays.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9(2) VALUE 0. 01 W-T PIC 9(3) VALUE 0. PROCEDURE DIVISION. MOVE 120 TO W-T. IF W-T IS GREATER THAN 100 ADD 1 TO W-N END-IF. MOVE 90 TO W-T. IF W-T IS GREATER THAN 100 ADD 1 TO W-N END-IF. DISPLAY W-N. STOP RUN.Answer
1
Only the first value is above the filter of the input procedure.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-S PIC XX VALUE "00". 01 W-N PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM UNTIL W-S = "10" ADD 1 TO W-N IF W-N IS GREATER THAN 2 MOVE "10" TO W-S END-IF END-PERFORM. DISPLAY W-S " " W-N. STOP RUN.Answer
10 3
The loop runs three times and the third pass sets the status that ends it.
-
Why does the input procedure close its own input file?
Answer
The program opened it, so the program closes it, and a sort loop does not leak descriptors
The procedure opened nothing; the file was opened before the SORT and closed inside the section.
-
A sort with an input and an output procedure reads an empty file. What happens?
Answer
Both procedures run, release and return nothing, the status is 00 and the report is empty
The last program printed zero counters, status 00 and a report of no bytes.
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