[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index

9.1.3 Resequencing Files with the USING and GIVING Phrases

If you only need to resequence a file, use the USING and GIVING phrases of the SORT statement. The USING phrase opens the input file, then reads and releases its records to the sort. The GIVING phrase opens and writes sorted records to the output file.

Note that you cannot manipulate data with either the USING or the GIVING phrases.

Consider this SORT statement:


SORT SORT-FILE ON ASCENDING KEY SORT-KEY-1
     USING INPUT-FILE GIVING OUTPUT-FILE.

It does the following:

  1. Opens INPUT-FILE
  2. Reads all records in INPUT-FILE and releases them to the sort
  3. Sorts the records in ascending sequence using the data in SORT-KEY-1
  4. Opens the output file and writes the sorted records to OUTPUT-FILE
  5. Closes all files used in the SORT statement

9.1.4 Manipulating Data Before and After Sorting with the INPUT PROCEDURE and OUTPUT PROCEDURE Phrases

You can manipulate data before and after sorting by using the INPUT PROCEDURE and OUTPUT PROCEDURE phrases, and sort only some of the information in a file. For example, these phrases allow you to use only those input records and/or input data fields you need.

The INPUT PROCEDURE phrase replaces the USING phrase when you want to manipulate data entering the sort. The SORT statement transfers control to the sections or paragraphs named in the INPUT PROCEDURE phrase. You then use COBOL statements to open and read files, and manipulate the data. You use the RELEASE statement to transfer records to the sort. After the last statement of the input procedure executes, control is given to the sort, and the records are subsequently sorted.

After the records are sorted, the SORT statement transfers control to the sections or paragraphs named in the OUTPUT PROCEDURE phrase. This phrase replaces the GIVING phrase when you want to manipulate data in the sort. You can use COBOL statements to open files and manipulate data. You use the RETURN statement to transfer records from the sort. For example, you can use the RETURN statement to retrieve the sorted records for printing a report.

Example 9-1 shows a sample sort using the INPUT and OUTPUT procedures.

Example 9-1 INPUT and OUTPUT PROCEDURE Phrases

IDENTIFICATION DIVISION.
PROGRAM-ID.  EX0901.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.

FILE-CONTROL.
    SELECT INPUT-FILE   ASSIGN TO "input.dat".
    SELECT OUTPUT-FILE  ASSIGN TO "output.dat".
    SELECT SORT-FILE    ASSIGN TO "sort.dat".

DATA DIVISION.
FILE SECTION.
FD  INPUT-FILE.
01  INPUT-RECORD     PIC X(100).
FD  OUTPUT-FILE.
01  OUTPUT-RECORD    PIC X(100).
SD  SORT-FILE.
01  SORT-RECORD      PIC X(100).
01  SORT-KEY-1       PIC XXX.
01  SORT-KEY-2       PIC XXX.

WORKING-STORAGE SECTION.

PROCEDURE DIVISION.
000-SORT SECTION.
010-DO-THE-SORT.
    SORT SORT-FILE ON ASCENDING KEY SORT-KEY-1
                   ON DESCENDING KEY SORT-KEY-2
                   INPUT PROCEDURE IS 050-RETRIEVE-INPUT
                                THRU 100-DONE-INPUT
                   OUTPUT PROCEDURE IS 200-WRITE-OUTPUT
                                THRU 230-DONE-OUTPUT.
    DISPLAY "END OF SORT".
    STOP RUN.
050-RETRIEVE-INPUT SECTION.
060-OPEN-INPUT.
    OPEN INPUT INPUT-FILE.
070-READ-INPUT.
    READ INPUT-FILE AT END
        CLOSE INPUT-FILE
        GO TO 100-DONE-INPUT.
    MOVE INPUT-RECORD TO SORT-RECORD.
***********************************************************
* You can add, change, or delete records before sorting   *
* using COBOL data manipulation                           *
* techniques.                                             *
***********************************************************
    RELEASE SORT-RECORD.
    GO TO 070-READ-INPUT.
100-DONE-INPUT SECTION.
110-EXIT-INPUT.
    EXIT.
200-WRITE-OUTPUT SECTION.
210-OPEN-OUTPUT.
    OPEN OUTPUT OUTPUT-FILE.
220-GET-SORTED-RECORDS.
    RETURN SORT-FILE AT END
        CLOSE OUTPUT-FILE
        GO TO 230-DONE-OUTPUT.
    MOVE SORT-RECORD TO OUTPUT-RECORD.
***********************************************************
* You can add, change, or delete sorted records           *
* using COBOL data manipulation                           *
* techniques.                                             *
***********************************************************
    WRITE OUTPUT-RECORD.
    GO TO 220-GET-SORTED-RECORDS.
230-DONE-OUTPUT SECTION.
240-EXIT-OUTPUT.
    EXIT.

You can combine the INPUT PROCEDURE with the GIVING phrases, or the USING with the OUTPUT PROCEDURE phrases. In Example 9-2, the USING phrase replaces the INPUT PROCEDURE phrase used in Example 9-1.

Note

You cannot access records released to the sort-file after execution of the SORT statement ends.

Example 9-2 USING Phrase Replaces INPUT PROCEDURE Phrase

.
.
.
PROCEDURE DIVISION.
000-SORT SECTION.
010-DO-THE-SORT.
    SORT SORT-FILE ON ASCENDING KEY SORT-KEY-1
                   ON DESCENDING KEY SORT-KEY-2
                   USING INPUT-FILE
                   OUTPUT PROCEDURE IS 200-WRITE-OUTPUT
                                  THRU 230-DONE-OUTPUT.
    DISPLAY "END OF SORT".
    STOP RUN.
200-WRITE-OUTPUT SECTION.
210-OPEN-OUTPUT.
    OPEN OUTPUT OUTPUT-FILE.
220-GET-SORTED-RECORDS.
    RETURN SORT-FILE AT END
        CLOSE OUTPUT-FILE
        GO TO 230-DONE-OUTPUT.
    MOVE SORT-RECORD TO OUTPUT-RECORD.
    WRITE OUTPUT-RECORD.
    GO TO 220-GET-SORTED-RECORDS.
230-DONE-OUTPUT SECTION.
240-EXIT-OUTPUT.
    EXIT.

9.1.5 Maintaining the Input Order of Records Using the WITH DUPLICATES IN ORDER Phrase

The sort orders data in the sequence specified in the ASCENDING KEY and DESCENDING KEY phrases. However, records with duplicate sort keys may not be written to the output file in the same sequence as they were read into it. The WITH DUPLICATES IN ORDER phrase ensures that any records with duplicate sort keys are in the same order in the output file as in the input file.

The following list shows the potential difference between sorting with the WITH DUPLICATES IN ORDER phrase and sorting without it:

Input File Sorted Without
Duplicates in Order
Sorted With
Duplicates in Order
Record Record Record
Name Data Name Data Name Data
JONES ABCD DAVIS LMNO DAVIS LMNO
DAVIS LMNO JONES EFGH JONES ABCD
WHITE STUV JONES ABCD JONES EFGH
JONES EFGH SMITH 1234 SMITH 1234
SMITH 1234 WHITE STUV WHITE STUV
WHITE WXYZ WHITE WXYZ WHITE WXYZ

If you omit the WITH DUPLICATES IN ORDER phrase, you cannot predict the order of records with duplicate sort keys. For example, the JONES records might not be in the same sequence as they were in the input file, but the WHITE records might be in the same order as in the input file.

In contrast, the WITH DUPLICATES IN ORDER phrase guarantees that records with duplicate sort keys remain in the same sequence as they were in the input file.

9.1.6 Specifying Non-ASCII Collating Sequences with the COLLATING SEQUENCE IS Alphabet-Name Phrase

This phrase lets you specify a collating sequence other than the ASCII default. You define collating sequences in the Environment Division SPECIAL-NAMES paragraph. A sequence specified in the COLLATING SEQUENCE IS phrase of the SORT statement overrides a sequence specified in the Environment Division PROGRAM COLLATING SEQUENCE IS phrase.

Example 9-3 shows the alphabet name NEWSEQUENCE overriding the EBCDIC-CODE collating sequence.

Example 9-3 Overriding the COLLATING SEQUENCE IS Phrase

ENVIRONMENT DIVISION.
OBJECT-COMPUTER.  FOO
      PROGRAM COLLATING SEQUENCE IS EBCDIC-CODE.
SPECIAL-NAMES.
    ALPHABET NEWSEQUENCE IS "ZYXWVUTSRQPONMLKJIHGFEDCBA"
    ALPHABET EBCDIC-CODE IS EBCDIC.
.
.
.
PROCEDURE DIVISION.
000-DO-THE-SORT.
    SORT SORT-FILE ON ASCENDING KEY
                      SORT-KEY-1
                      SORT-KEY-2
         COLLATING SEQUENCE IS NEWSEQUENCE
         USING INPUT-FILE GIVING OUTPUT-FILE.


Previous Next Contents Index