Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured COBOL Programming, Stern & Stern, 9th Edition

Similar presentations


Presentation on theme: "Structured COBOL Programming, Stern & Stern, 9th Edition"— Presentation transcript:

1 Structured COBOL Programming, Stern & Stern, 9th Edition
SORTING AND MERGING Structured COBOL Programming, Stern & Stern, 9th Edition

2 Structured COBOL Programming, Stern & Stern, 9th Edition
OBJECTIVES To Familiarize you with: 1. How files may be sorted within a COBOL program. 2. How to process a file during a SORT procedure before it is actually sorted. 3. How to process a file during a SORT procedure after it is sorted but before it is created as output. 4. How to use the MERGE verb for merging files. Structured COBOL Programming, Stern & Stern, 9th Edition

3 Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS THE SOFT FEATURE: AN OVERVIEW Format of the SORT Statement ASCENDING or DESCENDING KEY Coding a Simple SORT Procedure with the USING and GIVING Options PROCESSING DATA BEFORE AND/OR AFTER SORTING INPUT PROCEDURE OUTPUT PROCEDURE When to Use INPUT and/or OUTPUT Procedures THE MERGE STATEMENT Structured COBOL Programming, Stern & Stern, 9th Edition

4 Structured COBOL Programming, Stern & Stern, 9th Edition
THE SORT STATEMENT Records in files frequently must be sorted into specific sequences for updating, answering inquiries, or generating reports. Sorting is a common procedure used for arranging records into a specific order so that sequential processing can be performed. There are two techniques used for sorting files processed by COBOL programs. One is to use either a utility or a database management system's sort program. This sort program is completely separate from the COBOL program. Structured COBOL Programming, Stern & Stern, 9th Edition

5 FORMAT OF THE SORT STATEMENT
The second alternative is COBOL's SORT verb, which can make it very useful as part of a COBOL program. Often, a COBOL program will SORT a file prior to processing it. A simplified format follows: SORT file-name-1 {ON {DESCENDING}{ASCENDING} KEY data-name-1. .} USING file-name-2 GIVING file-name-3 Structured COBOL Programming, Stern & Stern, 9th Edition

6 ASCENDING or DESCENDING Key
The programmer must specify whether the key field is to be an ASCENDING KEY or a DESCENDING KEY, depending on which sequence is required: (1) ASCENDING: From lowest to highest (2) DESCENDING: From highest to lowest Records may be sorted using either numeric or nonnumeric key fields. Structured COBOL Programming, Stern & Stern, 9th Edition

7 ASCENDING or DESCENDING Key
Collating Sequence The two major codes used for representing data in a computer are EBCDIC primarily used on mainframes, and ASCII used widely on PCs. The sequence of characters from lowest to highest, which is referred to as the collating sequence, is somewhat different in EBCDIC and ASCII. Structured COBOL Programming, Stern & Stern, 9th Edition

8 Structured COBOL Programming, Stern & Stern, 9th Edition
Collating Sequence EBCDIC ASCII Low Blank Blank Special Characters Special Characters Lowercase letters a-z Integers 0-9 Uppercase letters A-Z Special Characters Integers Uppercase letters A-Z High Lowercase letters a-z Structured COBOL Programming, Stern & Stern, 9th Edition

9 Sequencing Records with More Than One SORT Key
The SORT verb may be used to sequence records with more than one key field. Suppose that we wish to sort fields in ascending alphabetic sequence by name, within each office. That is: Office number is the major sort field Level number is the intermediate sort field Name is the minor sort field Structured COBOL Programming, Stern & Stern, 9th Edition

10 Sequencing Records with More Than One SORT Key
The following is a SORT statement that sorts records into ascending alphabetic NAME sequence within LEVEL-NO within OFFICE-NO: SORT SORT-FILE ON ASCENDING KEY OFFICE-NO ON ASCENDING KEY LEVEL-NO ON ASCENDING KEY NAME USING PAYROLL-FILE-IN GIVING SORTED-PAYROLL-FILE-OUT Structured COBOL Programming, Stern & Stern, 9th Edition

11 ASCENDING / DESCENDING Key
If all key fields are to be sorted in ascending sequence we can code using the phrase ON ASCENDING KEY only once: SORT SORT-FILE ON ASCENDING KEY MAJOR-KEY INTERMEDIATE -KEY MINOR-KEY Structured COBOL Programming, Stern & Stern, 9th Edition

12 Coding a Simple SORT Procedure with the USING and GIVING Options
There are three major files used in a sort: 1. Input file: File of unsorted input records. 2. Work or sort file: File used to store records temporarily during the sorting process. 3. Output file: File of sorted output records. Structured COBOL Programming, Stern & Stern, 9th Edition

13 Coding a Simple SORT Procedure with the USING and GIVING Options
A sort file is usually assigned to a special work device--in the ENVIRONMENT DIVISION--indicated as SYSWORK in the following: SELECT UNSORTED-MASTERN-FILE ASSIGN TO DISK1. SELECT SORT-FILE ASSIGN TO SYSWORK. SELECT SORTED-MASTERN-FILE ASSIGN TO DISK2. Structured COBOL Programming, Stern & Stern, 9th Edition

14 Coding a Simple SORT Procedure with the USING/GIVING Options
The SORT-FILE is actually assigned to a temporary work area that is used during processing but not saved. Only the unsorted disk file and the sorted output disk file are assigned standard file-names so that they can be permanently stored. FDs are used in the DATA DIVISION to define and describe the input and output files in the usual way. The sort or work file is described with an SD entry. Structured COBOL Programming, Stern & Stern, 9th Edition

15 Structured COBOL Programming, Stern & Stern, 9th Edition
THE MERGE STATEMENT COBOL has a MERGE statement that will combine two or more files into a single file. Its format is similar to that of the SORT: MERGE file-name-1 {ON {ASCENDING} {DESCENDING} KEY data-name-1. .} USING file-name-2 {file-name-3} . . . {OUTPUT PROCEDURE IS procedure-name GIVING {file-name-4 [{THROUGH}{THRU} procedure-name-2]} Structured COBOL Programming, Stern & Stern, 9th Edition

16 Structured COBOL Programming, Stern & Stern, 9th Edition
THE MERGE STATEMENT File-name-1 is a work file designated as an SD. The key field specified as data-name-1, and any subsequent key fields, are defined within the SD. The first key field indicated in the ASCENDING or DESCENDING KEY clause of the MERGE is the major one, followed by intermediate and minor key fields. Rules for ASCENDING/DESCENDING KEY, USING, GIVING, and OUTPUT PROCEDURE are the same as for the SORT. Structured COBOL Programming, Stern & Stern, 9th Edition

17 Structured COBOL Programming, Stern & Stern, 9th Edition
THE MERGE STATEMENT With the USING clause, we indicate the files to be merged. Unlike the SORT, however, an INPUT PROCEDURE may not be specified with a MERGE statement: you may only process records after they have been merged, not before. The OUTPUT PROCEDURE has the same format as the SORT and may be used with a MERGE to: 1. Flag duplicate records as errors. 2. Ensure duplicate records. Structured COBOL Programming, Stern & Stern, 9th Edition

18 Structured COBOL Programming, Stern & Stern, 9th Edition
THE MERGE STATEMENT The MERGE statement automatically handles the opening, closing, and input/output (READ/WRITE functions) associated with the files. The files to be merged must each be in sequence by the key field. If ASCENDING KEY is specified, then the merged output file will have records in increasing order by key field. If DESCENDING KEY is specified, the merged output file will have key fields from high to low. Structured COBOL Programming, Stern & Stern, 9th Edition

19 Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY A. The SORT is used for sorting records in either ascending or descending order. 1. A program can simply sort a file on key fields: SORT file-name-1 {ON {ASCENDING}{DESCENDING} KEY data-name1...}... USING file-name-2 GIVING file-name-3 Structured COBOL Programming, Stern & Stern, 9th Edition

20 Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY a. File-name-1 is a work or sort file that is described with an SD (sort file description) in the FILE SECTION. b. The KEY field(s) to be sorted are data-names defined within the SD or sort file. c. Files can be sorted into ascending or descending sequence. d. Files can be sorted using more than one key field. The first field specified is the main sort field followed by intermediate and/or minor ones. Structured COBOL Programming, Stern & Stern, 9th Edition

21 Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY B. The MERGE statement can be used to merge two or more files. It is very similar to the SORT. It can have a USING and GIVING option or an OUTPUT PROCEDURE in place of the GIVING option. It cannot, however, have an INPUT PROCEDURE. Structured COBOL Programming, Stern & Stern, 9th Edition


Download ppt "Structured COBOL Programming, Stern & Stern, 9th Edition"

Similar presentations


Ads by Google