Sorting an indexed file

An indexed file is already in an order: the order of its record key. A report that needs another order cannot get it from the file, and the file itself cannot be reordered either, because the key is what puts the records where they are. The two operations this lesson shows are the two answers: sort the records on their way to the report, or build the file again under the key you want.

Read an indexed file into a sort

The input procedure of the sort reads the indexed file in key order and releases each record; the sort puts the records into name order, and the report is written from there:

IDENTIFICATION DIVISION. PROGRAM-ID. SIDX1. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUS-F ASSIGN TO "cus.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUS-ID FILE STATUS IS WS-CUS-ST. SELECT SORT-F ASSIGN TO "sortw.dat" FILE STATUS IS WS-SORT-ST. SELECT OUT-F ASSIGN TO "byname.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-OUT-ST. DATA DIVISION. FILE SECTION. FD CUS-F. 01 CUS-REC. 05 CUS-ID PIC X(3). 05 CUS-NAME PIC X(8). SD SORT-F. 01 SORT-REC. 05 S-ID PIC X(3). 05 S-NAME PIC X(8). FD OUT-F. 01 OUT-REC. 05 OUT-ID PIC X(3). 05 OUT-NAME PIC X(8). WORKING-STORAGE SECTION. 01 WS-CUS-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(2) VALUE 0. PROCEDURE DIVISION. OPEN OUTPUT CUS-F. MOVE "B20" TO CUS-ID. MOVE "Grace" TO CUS-NAME. WRITE CUS-REC. MOVE "A10" TO CUS-ID. MOVE "Ada" TO CUS-NAME. WRITE CUS-REC. MOVE "C30" TO CUS-ID. MOVE "Cy" TO CUS-NAME. WRITE CUS-REC. CLOSE CUS-F. OPEN INPUT CUS-F. SORT SORT-F ON ASCENDING KEY S-NAME INPUT PROCEDURE IS FEED GIVING OUT-F. DISPLAY "sort " WS-SORT-ST. DISPLAY "released " WS-RELEASED. 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-ID " " OUT-NAME END-READ END-PERFORM. CLOSE OUT-F. STOP RUN. FEED SECTION. PERFORM UNTIL WS-EOF = "Y" READ CUS-F NEXT AT END MOVE "Y" TO WS-EOF NOT AT END MOVE CUS-ID TO S-ID MOVE CUS-NAME TO S-NAME RELEASE SORT-REC ADD 1 TO WS-RELEASED END-READ END-PERFORM. CLOSE CUS-F.

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

sort 00
released 03
A10 Ada
C30 Cy
B20 Grace

The records went into the sort in identifier order and came out in name order. The count of released records is the one number worth printing: it has to be the number of records the report holds, and a difference between those two numbers is the first thing to look at when a report is short.

Rebuilding the file under another key

If the file itself has to be in name order, the only way is a second file whose RECORD KEY is the name. The program reads the old file, writes the new one and prints the status of every step:

IDENTIFICATION DIVISION. PROGRAM-ID. SIDX2. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT IN-F ASSIGN TO "cus.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS IN-ID FILE STATUS IS WS-IN-ST. SELECT OUT-F ASSIGN TO "byname.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS OUT-NAME FILE STATUS IS WS-OUT-ST. DATA DIVISION. FILE SECTION. FD IN-F. 01 IN-REC. 05 IN-ID PIC X(3). 05 IN-NAME PIC X(8). FD OUT-F. 01 OUT-REC. 05 OUT-ID PIC X(3). 05 OUT-NAME PIC X(8). WORKING-STORAGE SECTION. 01 WS-IN-ST PIC XX. 01 WS-OUT-ST PIC XX. 01 WS-EOF PIC X VALUE "N". 01 WS-N PIC 9(2) VALUE 0. PROCEDURE DIVISION. OPEN OUTPUT IN-F. MOVE "B20" TO IN-ID. MOVE "Grace" TO IN-NAME. WRITE IN-REC. MOVE "A10" TO IN-ID. MOVE "Ada" TO IN-NAME. WRITE IN-REC. MOVE "C30" TO IN-ID. MOVE "Cy" TO IN-NAME. WRITE IN-REC. CLOSE IN-F. OPEN INPUT IN-F. DISPLAY "open in " WS-IN-ST. OPEN OUTPUT OUT-F. DISPLAY "open out " WS-OUT-ST. PERFORM UNTIL WS-EOF = "Y" READ IN-F NEXT AT END MOVE "Y" TO WS-EOF NOT AT END MOVE IN-ID TO OUT-ID MOVE IN-NAME TO OUT-NAME WRITE OUT-REC ADD 1 TO WS-N DISPLAY "write " OUT-ID " " WS-OUT-ST END-READ END-PERFORM. CLOSE IN-F. CLOSE OUT-F. DISPLAY "copied " WS-N. OPEN INPUT OUT-F. DISPLAY "reopen " WS-OUT-ST. PERFORM UNTIL WS-OUT-ST = "10" READ OUT-F NEXT AT END MOVE "10" TO WS-OUT-ST NOT AT END DISPLAY "key " OUT-NAME " is " OUT-ID END-READ END-PERFORM. CLOSE OUT-F. STOP RUN.

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

open in 00
open out 00
write A10 00
write B20 00
write C30 00
copied 03
reopen 00
key Ada is A10
key Cy is C30
key Grace is B20

The writes went in identifier order and the reopened file returned the records in name order, which is the proof that the record key decides the order of an indexed file. Nothing in the program sorts: the index does it as the records arrive.

A rebuild is also the safe way to change the record layout of a file, and the pattern of the statuses above is what a job does with it: open the old file, open the new one, copy, close both, reopen the new one and read it back before the job reports success. The old file stays until the new one has been read.

A key that duplicates

The new key has a property the old one did not: two customers can share a name. The rebuild then loses a record, and the only trace is status 22:

IDENTIFICATION DIVISION. PROGRAM-ID. SIDX3. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT OUT-F ASSIGN TO "dupname.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS OUT-NAME FILE STATUS IS WS-OUT-ST. DATA DIVISION. FILE SECTION. FD OUT-F. 01 OUT-REC. 05 OUT-ID PIC X(3). 05 OUT-NAME PIC X(8). WORKING-STORAGE SECTION. 01 WS-OUT-ST PIC XX. 01 WS-N PIC 9(2) VALUE 0. 01 WS-LOST PIC 9(2) VALUE 0. PROCEDURE DIVISION. OPEN OUTPUT OUT-F. DISPLAY "open " WS-OUT-ST. MOVE "A10" TO OUT-ID. MOVE "Ada" TO OUT-NAME. WRITE OUT-REC. DISPLAY "write A10 " WS-OUT-ST. ADD 1 TO WS-N. MOVE "B20" TO OUT-ID. MOVE "Ada" TO OUT-NAME. WRITE OUT-REC. DISPLAY "write B20 " WS-OUT-ST. IF WS-OUT-ST = "22" ADD 1 TO WS-LOST DISPLAY "B20 is not in the new file" ELSE ADD 1 TO WS-N END-IF. CLOSE OUT-F. DISPLAY "written " WS-N " lost " WS-LOST. STOP RUN.

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

open 00
write A10 00
write B20 22
B20 is not in the new file
written 01 lost 01

The second Ada was refused and the rebuild continued, which is what a program that does not check the status of its writes does in production. A key that may repeat is declared with DUPLICATES after the RECORD KEY, and then both records are kept in the file with their arrival order inside the key; a program that needs the identifier of one of them reads by key and walks with READ ... NEXT.

The lesson closes the file handling part of the course. The next pages turn to the Report Writer, which is a way to describe the layout of a printed page instead of writing every line by hand.

Exercises

  1. Add a fourth customer to sidx1 whose name sorts before Ada. Where does the report put it?
  2. Declare the key of sidx3 with DUPLICATES and run it again. How many records does the new file hold, and what does the second write return?
  3. Print the number of records of byname.idx after the rebuild of sidx2 and compare it with the number of the input file.
  4. Change the sort key of sidx1 to S-ID. Which order does the report have now, and why is that order free?

Questions

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

  1. In which order does an indexed file return its records?

    Answer

    In the order of the record key

    The rebuilt file returned Ada, Cy, Grace although the writes were in identifier order.

  2. How does a report get an order that is not the record key?

    Answer

    The records are sorted on their way to the report

    The first program sorted by name and the file kept its identifier order.

  3. What does it take to have an indexed file in name order?

    Answer

    A second file whose RECORD KEY is the name

    The rebuild wrote a new indexed file with OUT-NAME as the record key.

  4. What status does a rebuild with two customers of the same name return for the second write?

    Answer

    22

    The third program printed write B20 22 and counted the record as lost.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC X(3) VALUE "B20". 01 W-B PIC X(3) VALUE "A10". 01 W-T PIC X(3) VALUE SPACES. PROCEDURE DIVISION. MOVE W-A TO W-T. MOVE W-B TO W-A. MOVE W-T TO W-B. DISPLAY W-A " " W-B. STOP RUN.

    Answer

    A10 B20

    The three moves swap the two keys through a temporary field.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9 VALUE 0. 01 W-S PIC XX VALUE "00". PROCEDURE DIVISION. PERFORM UNTIL W-S = "10" ADD 1 TO W-N IF W-N IS GREATER THAN 3 MOVE "10" TO W-S END-IF END-PERFORM. DISPLAY W-N " " W-S. STOP RUN.

    Answer

    4 10

    The fourth pass is the first with a count above three, and it sets the status.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC X(8) VALUE "Ada". 01 W-B PIC X(8) VALUE "Cy". PROCEDURE DIVISION. IF W-A IS LESS THAN W-B DISPLAY W-A ELSE DISPLAY W-B END-IF. STOP RUN.

    Answer

    Ada

    Ada sorts before Cy, so the first branch runs.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-C PIC 9(2) VALUE 3. 01 W-R PIC 9(2) VALUE 0. PROCEDURE DIVISION. MOVE W-C TO W-R. SUBTRACT 1 FROM W-R. DISPLAY W-R " " W-C. STOP RUN.

    Answer

    02 03

    The copy takes the value and the subtraction touches only the copy.

  9. Why does the rebuild print the status of every write?

    Answer

    A refused write does not stop the program, so a lost record is visible only in the status

    The duplicate program went on to write its summary although one record was refused.

  10. A job rebuilds a file under a new key and reports success. What check makes that report trustworthy?

    Answer

    The count of records written equals the count read, and the new file is read back

    The two counters of the rebuild and the reopen are what the page shows as proof.

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