350 likes | 503 Views
Sorting in COBOL. M. M. Pickard. Sorting? What is it?. Sorting? What is it?. Placing records into ascending or descending order based on the value of some key field(s) found within the records. Example: Sorting student records into order by social security number.
E N D
Sorting in COBOL M. M. Pickard
Sorting? What is it? • Placing records into ascending or descending order based on the value of some key field(s) found within the records. • Example: Sorting student records into order by social security number.
Sorting in the COBOL World • External to the program • Using Operating Systems commands • Inside the program • Using the SORT verb
Using the SORT verb • Consider the simplified syntax:SORT filename1 ON ASCENDING KEY dataname1DESCENDING USING filename2INPUT PROCEDURE procedure-name1 GIVING filename3 OUTPUT PROCEDURE procedure-name2
Example use of the SORT verb • SORT SORT-WORK-FILE ON ASCENDING KEY SORT-SSN USING STUDENT-FILE GIVING SORTED-STUDENT-FILE
Analysis of this Example • SORT SORT-WORK-FILE ON ASCENDING KEY SORT-SSN USING STUDENT-FILE GIVING SORTED-STUDENT-FILE • SORT-WORK-FILE is a sort work file. • STUDENT-FILE is the file containing data to be sorted (input to SORT). • SORTED-STUDENT-FILE is the file where sorted data is to be placed (output from the SORT). • The records are read by the SORT verb from STUDENT-FILE, sorted into order by SSN, and written by the SORT verb into SORTED-STUDENT-FILE.
Analysis of this Example (cont’d) • SORT SORT-WORK-FILE ON ASCENDING KEY SORT-SSN USING STUDENT-FILE GIVING SORTED-STUDENT-FILE • SORT-WORK-FILE must be defined using an SD. • SORT-SSN is the field on which the data is to be sorted, and must be defined within the records that are contained in SORT-WORK-FILE. • STUDENT-FILE must be defined using an FD. • SORTED-STUDENT-FILE must be defined using an FD. • In this example the format of the 3 files would be identical. • SORT automatically opens and closes files named in USING and GIVING clauses.
SORT Keys • Multiple keys are possible. • Example:SORT SORT-FILE ASCENDING SRT-NAME SRT-MAJOR DESCENDING SRT-GPA etcetera . . .
Multiple SORT Formats • SORT …USING…GIVING • SORT …USING…OUTPUT PROCEDURE • SORT …INPUT PROCEDURE …GIVING • SORT …INPUT PROCEDURE …OUTPUT PROCEDURE
SORT … USING … GIVING • Use this form when to sort allthe records in a given file into a specified order so that a file of sorted records is produced. • Similar to an external sort.
SORT with an input procedure • Example: SORT SORT-FILE DESCENDING KEY SRT-STATE, SRT-NAME INPUT PROCEDURE 100-SELECT GIVING SORTED-PAY-FILE.
SORT with an input procedure (continued) • SORT SORT-FILE DESCENDING KEY SRT-STATE, SRT-NAME INPUT PROCEDURE 100-SELECT GIVING SORTED-PAY-FILE. • The duty of the input procedure is to furnish records to be sorted. This is done by means of a new verb, RELEASE. Example: RELEASE SORT-REC.
SORT with an input procedure (continued) • SORT SORT-FILE DESCENDING KEY SRT-STATE, SRT-NAME INPUT PROCEDURE 100-SELECT GIVING SORTED-PAY-FILE. • The input procedure (100-SELECT) in this case contains a loop which selects certain records to be RELEASEd to the SORT.
Example input procedure 100-SELECT. OPEN INPUT PAY-FILE READ PAY-FILE AT END MOVE 1 TO EOF-PAY-SW PERFORM UNTIL EOF-ON-PAYFILE IF PAY-ANNUAL > 50000 MOVE PAY-REC TO SORT-REC RELEASE SORT-REC END-IF READ PAY-FILE AT END MOVE 1 TO EOF-SW END-PERFORM CLOSE PAY-FILE.
Notes on Input Procedure • An input procedure logically MUST contain a RELEASE statement. • Note that the SORT verb doesn’t automatically open or close files referenced in input procedure.
SORT with an output procedure • Example: SORT SORT-FILE DESCENDING KEY SRT-STATE, SRT-NAME USING PAY-FILE OUTPUT PROCEDURE IS 500-REPORT-PAY END-SORT
SORT with an output procedure (continued) • SORT SORT-FILE DESCENDING KEY SRT-STATE, SRT-NAME USING PAY-FILE OUTPUT PROCEDURE IS 500-REPORT-PAY • The duty of the output procedure is to process records that have been sorted. This is done using a new verb, RETURN. Example: RETURN SORT-FILE
SORT with an output procedure (continued) • SORT SORT-FILE DESCENDING KEY SRT-STATE, SRT-NAME USING PAY-FILE OUTPUT PROCEDURE IS 500-REPORT-PAY • The output procedure (500-REPORT-PAY) in this case contains a loop which returns sorted records one at a time to be printed in a report.
Example output procedure 500-REPORT-PAY. OPEN OUTPUT PAY-REPORT-FILE. PERFORM 550-REPORT-HEADING RETURN SORT-FILE AT END MOVE 1 TO EOF-SRT-SW PERFORM UNTIL EOF-ON-SORTFILE PERFORM 560-CREATE-DETAIL-LINE WRITE PAY-RPT-REC RETURN SORT-FILE AT END MOVE 1 TO EOF-SW END-PERFORM PERFORM 570-REPORT-FOOTER CLOSE PAY-REPORT-FILE.
Notes on Output Procedure • An output procedure logically MUST contain a RETURN statement. • Note that the SORT verb doesn’t automatically open or close files referenced in output procedure. • SORTed records are returned one at a time to be processed.
Further Notes on Input/Output Procedures • The RELEASE statement acts like a WRITE. • Note that it references a record. • The RETURN statement acts like a READ. • Note that it references a file.
Some of the Finer Points . . . • Beware of what follows the SORT. • Remember, the SORT statement, like a PERFORM, is active all during the input and output procedures as well as the sort process itself. • When the SORT is done, control passes to the next statement, whatever it is. • The sort work file does not have to be assigned in Open-VMS DCL.
Remember the Multiple SORT Formats? • SORT …USING…GIVING • SORT …USING…OUTPUT PROCEDURE • SORT …INPUT PROCEDURE …GIVING • SORT …INPUT PROCEDURE …OUTPUT PROCEDURE
Choosing the Appropriate SORT statement Format • When is the SORT … USING … GIVING appropriate?
Choosing the Appropriate SORT statement Format • Q: When is the SORT … USING … GIVING … appropriate? • A: When all of the records in a file are to be sorted and written to an output file in sorted order, and no special processing is required either before or after sorting.
Choosing the Appropriate SORT statement Format • Q: When is the SORT … INPUT PROCEDURE … GIVING … appropriate?
Choosing the Appropriate SORT statement Format • Q: When is the SORT … INPUT PROCEDURE … GIVING … appropriate? • A: When there is need to process data before sorting. Examples: • When only certain records are to be selected for sorting. • When a new record is to be created before sorting.
Choosing the Appropriate SORT statement Format • Q: When is the SORT … USING … OUTPUT PROCEDURE … appropriate?
Choosing the Appropriate SORT statement Format • Q: When is the SORT … USING … OUTPUT PROCEDURE … appropriate? • A: When there is a need for data manipulation following the SORT, such as . . . • When a report is needed instead of a file. • When sorted data is used to create a selective file.
Choosing the Appropriate SORT statement Format • Q: When is the SORT … INPUT PROCEDURE … OUTPUT PROCEDURE … appropriate? • A: When there is a need for data manipulation preceding and following the SORT, such as . . . • When only selected records are needed to produce a report in a given order. • When multiple files are used to create data that is sorted and then used to create several output files. • And so on . . .
Choosing the Appropriate SORT statement Format • Note that any situation can be covered by the SORT … INPUT PROCEDURE … OUTPUT PROCEDURE … option. • However, choosing the correct SORT configuration can have a very significant effect on efficiency.
In Conclusion • Did this discussion make you out of SORTs? • Are you confident that you can use SORT correctly? Choose one: • Yes, very. • SORT of. • No, not at all.