beautypg.com

Compaq COBOL AAQ2G1FTK User Manual

Page 296

background image

Using the SORT and MERGE Statements
9.3 Sample Programs Using the SORT and MERGE Statements

Example 9–11 (Cont.) Creating a New Sort Key

03 INCOME-FOURTH-QUARTER

PIC 9(5)V99.

03 ORDER-COUNT

PIC 9(2).

03 ORDERS OCCURS 1 TO 7 TIMES

DEPENDING ON ORDER-COUNT.

05

ORDER-DATE

PIC 9(6).

05

FILLER

PIC X(59).

SD

SORT-FILE
RECORD VARYING FROM 106 TO 496 CHARACTERS
DEPENDING ON SORT-LENGTH.

01

SORT-REC.

03

SORT-ANNUAL-INCOME

PIC 9(6).

03

SORT-REST-OF-RECORD

PIC X(490).

FD

OUT-FILE
RECORD VARYING FROM 106 TO 496 CHARACTERS
DEPENDING ON OUT-LENGTH.

01

OUT-REC

PIC X(496).

WORKING-STORAGE SECTION.
01

IN-LENGTH

PIC 9(3) COMP.

01

SORT-LENGTH

PIC 9(3) COMP.

01

OUT-LENGTH

PIC 9(3) COMP.

PROCEDURE DIVISION.
000-START SECTION.
005-SORT-HERE.

SORT SORT-FILE

ON DESCENDING SORT-ANNUAL-INCOME
INPUT PROCEDURE 010-GET-INPUT

THRU 070-DONE-INPUT

OUTPUT PROCEDURE 100-WRITE-OUTPUT.

DISPLAY "END OF PROGRAM SORTE".
STOP RUN.

010-GET-INPUT SECTION.
020-OPEN-INPUT.

OPEN INPUT INFILE.

030-READ-INPUT.

READ INFILE AT END

CLOSE INFILE
GO TO 070-DONE-INPUT.

040-ADD-INCOME.

ADD INCOME-FIRST-QUARTER

INCOME-SECOND-QUARTER
INCOME-THIRD-QUARTER
INCOME-FOURTH-QUARTER
GIVING SORT-ANNUAL-INCOME.

050-CREATE-SORT-REC.

ADD 6 IN-LENGTH GIVING SORT-LENGTH.
MOVE INREC TO SORT-REST-OF-RECORD.
RELEASE SORT-REC.
GO TO 030-READ-INPUT.

070-DONE-INPUT SECTION.
080-EXIT.

EXIT.

100-WRITE-OUTPUT SECTION.
110-OPEN.

OPEN OUTPUT OUT-FILE.

(continued on next page)

9–18 Using the SORT and MERGE Statements