beautypg.com

Compaq COBOL AAQ2G1FTK User Manual

Page 293

background image

Using the SORT and MERGE Statements

9.3 Sample Programs Using the SORT and MERGE Statements

Example 9–9 (Cont.) Using the INPUT PROCEDURE and OUTPUT

PROCEDURE Phrases

PROCEDURE DIVISION.
000-START SECTION.
005-DO-THE-SORT.

SORT SORT-FILE ON ASCENDING KEY

S-KEY-1
S-KEY-2

INPUT PROCEDURE IS 010-GET-INPUT

THRU 200-DONE-INPUT-GET

OUTPUT PROCEDURE IS 300-CREATE-OUTPUT-FILE

THRU 600-DONE-CREATE.

********************************************************
*

Notice the use of DISPLAY and record counters to

*

*

produce sort statistics.

*

********************************************************

DISPLAY "TOTAL FIRST-FILE RECORDS IS

" FILE01-COUNT.

DISPLAY "TOTAL SECOND-FILE RECORDS IS

" FILE02-COUNT.

DISPLAY "TOTAL NUMBER OF SORTED RECORDS IS

" SORT-COUNT.

DISPLAY "TOTAL NUMBER OF OUTPUT RECORDS IS

" OUTPUT-COUNT.

************************************************************
*

At this point, you could transfer control to another

*

*

section of the program

and continue processing.

*

************************************************************

DISPLAY "END OF PROGRAM SORTC".
STOP RUN.

010-GET-INPUT SECTION.
050-OPEN-FILES.

OPEN INPUT FIRST-FILE.

100-READ-FIRST-FILE.

READ FIRST-FILE AT END

CLOSE FIRST-FILE
OPEN INPUT SECOND-FILE
GO TO 150-READ-SECOND-FILE.

ADD 1 TO FILE01-COUNT.
IF R1-STATUS-CODE = "D"

GO TO 100-READ-FIRST-FILE.

RELEASE SORT-REC FROM RECORD1.
GO TO 100-READ-FIRST-FILE.

150-READ-SECOND-FILE.

READ SECOND-FILE AT END

CLOSE SECOND-FILE
GO TO 200-DONE-INPUT-GET.

ADD 1 TO FILE02-COUNT.
IF R2-STATUS-CODE = "D"

GO TO 150-READ-SECOND-FILE.

RELEASE SORT-REC FROM RECORD2.
GO TO 150-READ-SECOND-FILE.

200-DONE-INPUT-GET SECTION.
250-EXIT-PARAGRAPH.

EXIT.

300-CREATE-OUTPUT-FILE SECTION.
350-OPEN-OUTPUT.

OPEN OUTPUT OUTPUT-FILE.

400-READ-SORT-FILE.

RETURN SORT-FILE AT END

PERFORM 500-WRITE-THE-OUTPUT
CLOSE OUTPUT-FILE
GO TO 600-DONE-CREATE.

ADD 1 TO SORT-COUNT.
IF INITIAL-SORT-READ = "Y"

MOVE SORT-REC TO SAVE-SORT-REC

(continued on next page)

Using the SORT and MERGE Statements 9–15