1 / 33

Sorting in COBOL

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.

marin
Télécharger la présentation

Sorting in COBOL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting in COBOL M. M. Pickard

  2. Sorting? What is it?

  3. 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.

  4. Sorting in the COBOL World • External to the program • Using Operating Systems commands • Inside the program • Using the SORT verb

  5. 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

  6. Example use of the SORT verb • SORT SORT-WORK-FILE ON ASCENDING KEY SORT-SSN USING STUDENT-FILE GIVING SORTED-STUDENT-FILE

  7. 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.

  8. 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.

  9. SORT Keys • Multiple keys are possible. • Example:SORT SORT-FILE ASCENDING SRT-NAME SRT-MAJOR DESCENDING SRT-GPA etcetera . . .

  10. Multiple SORT Formats • SORT …USING…GIVING • SORT …USING…OUTPUT PROCEDURE • SORT …INPUT PROCEDURE …GIVING • SORT …INPUT PROCEDURE …OUTPUT PROCEDURE

  11. 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.

  12. SORT with an input procedure • Example: SORT SORT-FILE DESCENDING KEY SRT-STATE, SRT-NAME INPUT PROCEDURE 100-SELECT GIVING SORTED-PAY-FILE.

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. 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

  18. 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

  19. 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.

  20. 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.

  21. 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.

  22. 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.

  23. 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.

  24. Remember the Multiple SORT Formats? • SORT …USING…GIVING • SORT …USING…OUTPUT PROCEDURE • SORT …INPUT PROCEDURE …GIVING • SORT …INPUT PROCEDURE …OUTPUT PROCEDURE

  25. Choosing the Appropriate SORT statement Format • When is the SORT … USING … GIVING appropriate?

  26. 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.

  27. Choosing the Appropriate SORT statement Format • Q: When is the SORT … INPUT PROCEDURE … GIVING … appropriate?

  28. 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.

  29. Choosing the Appropriate SORT statement Format • Q: When is the SORT … USING … OUTPUT PROCEDURE … appropriate?

  30. 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.

  31. 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 . . .

  32. 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.

  33. 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.

More Related