|
HP COBOL User Manual
Example 9-9 shows how to use the INPUT PROCEDURE and OUTPUT
PROCEDURE phrases.
Example 9-9 Using the INPUT PROCEDURE and
OUTPUT PROCEDURE Phrases |
IDENTIFICATION DIVISION.
PROGRAM-ID. SORTC.
*********************************************************
* This program shows how to use the INPUT *
* PROCEDURE and OUTPUT PROCEDURE phrases of the *
* SORT statement. Input to the sort is two files *
* containing the same type of data. Records with *
* a "D" status-code are not released to the sort. *
* The program eliminates duplicate records by *
* adding their amounts to the amount in the first *
* record with the same account number. Only records *
* with unique account numbers are written to *
* the output file. The fields to be sorted are *
* S-KEY-1 and S-KEY-2. The sort sequence is amount *
* within account number. *
*********************************************************
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FIRST-FILE ASSIGN TO "FILE01".
SELECT SECOND-FILE ASSIGN TO "FILE02".
SELECT OUTPUT-FILE ASSIGN TO "OUTFIL".
SELECT SORT-FILE ASSIGN TO "SRTFIL".
DATA DIVISION.
FILE SECTION.
SD SORT-FILE.
01 SORT-REC.
03 S-KEY-1.
05 S-ACCOUNT-NUM PIC X(8).
03 FILLER PIC X(32).
03 S-KEY-2.
05 S-AMOUNT PIC S9(5)V99.
03 FILLER PIC X(53).
FD FIRST-FILE
LABEL RECORDS ARE STANDARD.
01 RECORD1.
03 FILLER PIC X(99).
03 R1-STATUS-CODE PIC X.
FD SECOND-FILE
LABEL RECORDS ARE STANDARD.
01 RECORD2.
03 FILLER PIC X(99).
03 R2-STATUS-CODE PIC X.
FD OUTPUT-FILE
LABEL RECORDS ARE STANDARD.
01 OUT-REC PIC X(100).
WORKING-STORAGE SECTION.
01 INITIAL-SORT-READ PIC X VALUE "Y".
01 FILE01-COUNT PIC 9(5) VALUE ZEROES.
01 FILE02-COUNT PIC 9(5) VALUE ZEROES.
01 SORT-COUNT PIC 9(5) VALUE ZEROES.
01 OUTPUT-COUNT PIC 9(5) VALUE ZEROES.
01 SAVE-SORT-REC.
03 SR-ACCOUNT-NUM PIC X(8).
03 FILLER PIC X(32).
03 SR-AMOUNT PIC S9(5)V99.
03 FILLER PIC X(53).
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
MOVE "N" TO INITIAL-SORT-READ
GO TO 400-READ-SORT-FILE.
450-COMPARE-ACCOUNT-NUM.
IF S-ACCOUNT-NUM = SR-ACCOUNT-NUM
ADD S-AMOUNT TO SR-AMOUNT
GO TO 400-READ-SORT-FILE.
500-WRITE-THE-OUTPUT.
MOVE SAVE-SORT-REC TO OUT-REC.
WRITE OUT-REC.
ADD 1 TO OUTPUT-COUNT.
550-GET-A-REC.
MOVE SORT-REC TO SAVE-SORT-REC.
GO TO 400-READ-SORT-FILE.
600-DONE-CREATE SECTION.
650-EXIT-PARAGRAPH.
EXIT.
|
Example 9-10 shows how to use the COLLATING SEQUENCE IS phrase.
Example 9-10 Using the COLLATING SEQUENCE IS
Phrase |
IDENTIFICATION DIVISION.
PROGRAM-ID. SORTD.
**************************************************
* This program sorts a file into a non-ASCII *
* collating sequence. The collating sequence *
* is defined by the alphabet-name MYSEQUENCE *
* in the SPECIAL-NAMES paragraph of the *
* ENVIRONMENT DIVISION. *
* The collating sequence is: *
* 1. The letters A to Z *
* 2. The digits 0 to 9 *
**************************************************
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
ALPHABET MYSEQUENCE IS
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ".
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT-FILE ASSIGN TO "INPFIL".
SELECT OUTPUT-FILE ASSIGN TO "OUTFIL".
SELECT SORT-FILE ASSIGN TO "SRTFIL".
DATA DIVISION.
FILE SECTION.
SD SORT-FILE.
01 SORT-REC.
03 S-KEY-1.
05 S-ACCOUNT-NAME PIC X(23).
03 S-KEY-2.
05 S-AMOUNT PIC S9(5)V99.
FD INPUT-FILE
LABEL RECORDS ARE STANDARD.
01 IN-REC PIC X(30).
FD OUTPUT-FILE
LABEL RECORDS ARE STANDARD.
01 OUT-REC PIC X(30).
PROCEDURE DIVISION.
000-DO-THE-SORT.
SORT SORT-FILE ON ASCENDING KEY
S-KEY-1
S-KEY-2
COLLATING SEQUENCE IS MYSEQUENCE
USING INPUT-FILE GIVING OUTPUT-FILE.
************************************************************
* At this point, you could transfer control to another *
* section of the program and continue processing. *
************************************************************
DISPLAY "END OF PROGRAM SORTD".
STOP RUN.
|
Example 9-11 is an example of creating a new sort key.
Example 9-11 Creating a New Sort Key |
IDENTIFICATION DIVISION.
PROGRAM-ID. SORTE.
************************************************
* This program increases the size of the *
* variable input records by a new six- *
* character field and uses this field *
* as the sort key. *
************************************************
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INFILE ASSIGN TO "INFILE".
SELECT SORT-FILE ASSIGN TO "SRTFIL".
SELECT OUT-FILE ASSIGN TO "OUTFILE".
DATA DIVISION.
FILE SECTION.
FD INFILE
RECORD VARYING FROM 100 TO 490 CHARACTERS
DEPENDING ON IN-LENGTH.
01 INREC.
03 ACCOUNT PIC 9(5).
03 INCOME-FIRST-QUARTER PIC 9(5)V99.
03 INCOME-SECOND-QUARTER PIC 9(5)V99.
03 INCOME-THIRD-QUARTER PIC 9(5)V99.
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.
120-WRITE.
RETURN SORT-FILE AT END
CLOSE OUT-FILE
GO TO 130-DONE.
MOVE SORT-LENGTH TO OUT-LENGTH.
WRITE OUT-REC.
GO TO 120-WRITE.
130-DONE.
EXIT.
|
Example 9-12 merges three identically sequenced files into one file.
Example 9-12 Merging Files |
IDENTIFICATION DIVISION.
PROGRAM-ID. MERGE01.
******************************************************
* This program merges three identically sequenced *
* regional sales files into one total sales file. *
* The program adds sales amounts and writes one *
* record for each product code. *
******************************************************
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT REGION1-SALES ASSIGN TO "REG1SLS".
SELECT REGION2-SALES ASSIGN TO "REG2SLS".
SELECT REGION3-SALES ASSIGN TO "REG3SLS".
SELECT MERGE-FILE ASSIGN TO "MRGFILE".
SELECT TOTAL-SALES ASSIGN TO "TOTLSLS".
DATA DIVISION.
FILE SECTION.
FD REGION1-SALES
LABEL RECORDS ARE STANDARD.
01 REGION1-RECORD PIC X(100).
FD REGION2-SALES
LABEL RECORDS ARE STANDARD.
01 REGION2-RECORD PIC X(100).
FD REGION3-SALES
LABEL RECORDS ARE STANDARD.
01 REGION3-RECORD PIC X(100).
SD MERGE-FILE.
01 MERGE-REC.
03 M-REGION-CODE PIC XX.
03 M-PRODUCT-CODE PIC X(10).
03 M-SALES-AMT PIC S9(7)V99.
03 FILLER PIC X(79).
FD TOTAL-SALES
LABEL RECORDS ARE STANDARD.
01 TOTAL-RECORD PIC X(100).
WORKING-STORAGE SECTION.
01 INITIAL-READ PIC X VALUE "Y".
01 THE-COUNTERS.
03 PRODUCT-AMT PIC S9(7)V99.
03 REGION1-AMT PIC S9(9)V99.
03 REGION2-AMT PIC S9(9)V99.
03 REGION3-AMT PIC S9(9)V99.
03 TOTAL-AMT PIC S9(11)V99.
01 SAVE-MERGE-REC.
03 S-REGION-CODE PIC XX.
03 S-PRODUCT-CODE PIC X(10).
03 S-SALES-AMT PIC S9(7)V99.
03 FILLER PIC X(79).
PROCEDURE DIVISION.
000-START SECTION.
010-MERGE-FILES.
OPEN OUTPUT TOTAL-SALES.
MERGE MERGE-FILE ON ASCENDING KEY M-PRODUCT-CODE
USING REGION1-SALES REGION2-SALES REGION3-SALES
OUTPUT PROCEDURE IS 020-BUILD-TOTAL-SALES
THRU 100-DONE-TOTAL-SALES.
DISPLAY "TOTAL SALES FOR REGION 1 " REGION1-AMT.
DISPLAY "TOTAL SALES FOR REGION 2 " REGION2-AMT.
DISPLAY "TOTAL SALES FOR REGION 3 " REGION3-AMT.
DISPLAY "TOTAL ALL SALES " TOTAL-AMT.
CLOSE TOTAL-SALES.
DISPLAY "END OF PROGRAM MERGE01".
STOP RUN.
020-BUILD-TOTAL-SALES SECTION.
030-GET-MERGE-RECORDS.
RETURN MERGE-FILE AT END
MOVE PRODUCT-AMT TO S-SALES-AMT
WRITE TOTAL-RECORD FROM SAVE-MERGE-REC
GO TO 100-DONE-TOTAL-SALES.
IF INITIAL-READ = "Y"
MOVE "N" TO INITIAL-READ
MOVE MERGE-REC TO SAVE-MERGE-REC
PERFORM 050-TALLY-AMOUNTS
GO TO 030-GET-MERGE-RECORDS.
040-COMPARE-PRODUCT-CODE.
IF M-PRODUCT-CODE = S-PRODUCT-CODE
PERFORM 050-TALLY-AMOUNTS
GO TO 030-GET-MERGE-RECORDS.
MOVE PRODUCT-AMT TO S-SALES-AMT.
MOVE ZEROES TO PRODUCT-AMT.
WRITE TOTAL-RECORD FROM SAVE-MERGE-REC.
MOVE MERGE-REC TO SAVE-MERGE-REC.
GO TO 040-COMPARE-PRODUCT-CODE.
050-TALLY-AMOUNTS.
ADD M-SALES-AMT TO PRODUCT-AMT TOTAL-AMT.
IF M-REGION-CODE = "01"
ADD M-SALES-AMT TO REGION1-AMT.
IF M-REGION-CODE = "02"
ADD M-SALES-AMT TO REGION2-AMT.
IF M-REGION-CODE = "03"
ADD M-SALES-AMT TO REGION3-AMT.
100-DONE-TOTAL-SALES SECTION.
120-DONE.
EXIT.
|
|