Presentation is loading. Please wait.

Presentation is loading. Please wait.

PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert.

Similar presentations


Presentation on theme: "PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert."— Presentation transcript:

1 PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College “Copyright @ 2000 John Wiley & Sons, In. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express permission of the copyright owner is unlawful. Request for further information should be addressed to the permissions Department, John Wily & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of these programs or from the use of the information contained herein.”

2 Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER 14 SORTING AND MERGING

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

4 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

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

6 Structured COBOL Programming, Stern & Stern, 9th Edition 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

7 Structured COBOL Programming, Stern & Stern, 9th Edition 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.

8 Structured COBOL Programming, Stern & Stern, 9th Edition 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.

9 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 0-9 Uppercase letters A-Z High Lowercase letters a-z

10 Structured COBOL Programming, Stern & Stern, 9th Edition 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

11 Structured COBOL Programming, Stern & Stern, 9th Edition 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

12 Structured COBOL Programming, Stern & Stern, 9th Edition 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

13 Structured COBOL Programming, Stern & Stern, 9th Edition 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.

14 Structured COBOL Programming, Stern & Stern, 9th Edition 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.

15 Structured COBOL Programming, Stern & Stern, 9th Edition 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.

16 Structured COBOL Programming, Stern & Stern, 9th Edition QUESTIONS! ??

17 Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 1. Suppose we want EMPLOYEE-FILE records in alphabetic order by NAME within DISTRICT within TERRITORY, all in ascending sequence. –The output file is called SORTED-EMPLOYEE- FILE. Complete the following SORT statement: SORT WORK-FILE... Solution: ON ASCENDING KEY TERRITORY ON ASCENDING KEY DISTRICT ON ASCENDING KEY NAME USING EMPLOYEE-FILE GIVING SORTED-EMPLOYEE-FILE

18 Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 2. How many files are required in a SORT routine? Describe these files. Solution: Three; input--unsorted; work or sort file-- temporary; output--sorted

19 Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 3. The work or sort file is defined as an ______ in the DATA DIVISION. Solution: SD (SORT DESCRIPTION)

20 Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 4. Suppose we have an FD called NET-FILE- IN, an SD called NET-FILE, and an FD called NET-FILE-OUT. We want NET-FILE-OUT sorted into ascending DEPT-NO sequence. Code the PROCEDURE DIVISION entry. Solution: SORT NET-FILE ON ASCENDING KEY DEPT-NO USING NET-FILE-IN GIVING NET-FILE-OUT

21 Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 5. In Question 4, DEPT-NO must be a field defined within the (SD/FD) file Solution: SD

22 Structured COBOL Programming, Stern & Stern, 9th Edition PROCESSING DATA BEFORE AND/OR AFTER SORTING The SORT statement can be used in conjunction with procedures that process records before and after they are sorted. –The SORT statement is used to perform some processing of incoming records just before they are sorted. For incoming records, this is accomplished with an INPUT PROCEDURE clause in place of the USING clause using the following format:

23 Structured COBOL Programming, Stern & Stern, 9th Edition INPUT PROCEDURE Expanded Format: SORT file-name-1 {ON {ASCENDING}{DESCENDING} KEY data-name-1....}... {INPUT PROCEDURE IS procedure-name-1 [{THRU}{THROUGH} procedure-name-2] {USING file-name-2....} GIVING file-name-3 The INPUT PROCEDURE processes data from the incoming file prior to sorting.

24 Structured COBOL Programming, Stern & Stern, 9th Edition INPUT PROCEDURE We must release records to the sort file in an INPUT PROCEDURE. –With a USING option, this is done for us automatically. Note that the RELEASE verb is followed by a record-name, just like the WRITE statement. With COBOL 85, the INPUT PROCEDURE may reference either a section or a paragraph.

25 Structured COBOL Programming, Stern & Stern, 9th Edition INPUT PROCEDURE Regardless of whether you are using COBOL 85 or COBOL 74, an INPUT PROCEDURE : opens the input file processes input records, and releases them to the sort file. –After all input records are processed, the input file is closed. The format for the RELEASE is: RELEASE sort-record-name-1 [FROM identifier-1]...

26 Structured COBOL Programming, Stern & Stern, 9th Edition INPUT PROCEDURE RELEASE is the verb to write records to a sort file: Examples: MOVE IN-REC TO SORT-REC RELEASE SORT-REC. OR RELEASE SORT-REC FROM IN-REC.

27 Structured COBOL Programming, Stern & Stern, 9th Edition MORE QUESTIONS

28 Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST The following illustrates the problem definition for a program that is to sort a disk file. UNSORTED-FILE Record Layout (Input) FieldSize Type PART-NO-IN 5 Alphanumeric QTY-IN 5 Alphanumeric DEPT-IN 2 Alphanumeric ----------------------------------------------------------------- SORTED-FILE Record Layout (Output) DEPT-OUT 2 Alphanumeric PART-NO-OUT 5 Alphanumeric QTY-OUT 5 Alphanumeric

29 Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST: Consider the following FILE SECTION: DATA DIVISION. FILE SECTION. FD UNSORTED-FILE LABEL RECORDS ARE STANDARD. 01 REC-1. 05 PART-NO-INPIC X(5). 05 QTY-INPIC X(5). 05 DEPT-INPIC X(2). SD SORT-FILE. 01 SORT-REC. 05 S-DEPTPIC X(2). 05 S-PART-NOPIC X(5). 05 S-QTYPIC X(5). FD SORTED-FILE LABEL RECORDS ARE STANDARD. 01 REC-2PIC X(12).

30 Structured COBOL Programming, Stern & Stern, 9th Edition SELF TEST 1. (T or F) It would be possible, although inefficient, to (1) first sort the input and produce a sorted master, and (2) then code a separate module to read from the sorted master, moving the data in a rearranged format to a new sorted master. SOLUTION: T

31 Structured COBOL Programming, Stern & Stern, 9th Edition SELF TEST 2. (T or F) It would be more efficient to use an INPUT PROCEDURE for this problem. SOLUTION: T

32 Structured COBOL Programming, Stern & Stern, 9th Edition SELF TEST 3. Code the SORT Statement. SOLUTION: SORT SORT-FILE ON ASCENDING KEY S-DEPT INPUT PROCEDURE A000-REARRANGE GIVING SORTED-FILE STOP RUN.

33 Structured COBOL Programming, Stern & Stern, 9th Edition SELF TEST 4. Code the INPUT PROCEDURE SECTION assuming that you are using a COBOL 85 compiler. SOLUTION: A00-REARRANGE. OPEN INPUT UNSORTED-FILE PERFORM UNTIL NO-MORE-RECORDS READ UNSORTED-FILE AT END MOVE ‘NO’ TO ARE-THERE-MORE- RECORDS NOT AT END PERFORM A200-CALC-RTN END-READ END-PERFORM CLOSE UNSORTED-FILE.

34 Structured COBOL Programming, Stern & Stern, 9th Edition SELF TEST SOLUTION 4 (continued) A200-CALC-RTN. MOVE PART-NO-IN TO S-PART-NO MOVE QTY-IN TO S-QTY MOVE DEPT-IN TO S-DEPT RELEASE SORT-REC.

35 Structured COBOL Programming, Stern & Stern, 9th Edition OUTPUT PROCEDURE After records have been sorted, they are placed in the sort file in the sequence required. –If the GIVING option is used, then the sorted records are automatically written onto the output file after they are sorted. In order to process the sorted records prior to, or perhaps even instead of, placing them in the output file, we would use an OUTPUT PROCEDURE instead of the GIVING option. –This OUTPUT PROCEDURE is very similar to the INPUT PROCEDURE.

36 Structured COBOL Programming, Stern & Stern, 9th Edition OUTPUT PROCEDURE The full format for the SORT, including both INPUT and OUTPUT PROCEDURE options, is as follows: SORT file-name-1 {ON{DESCENDING}{ASCENDING} KEY data-name-1... }… {INPUT PROCEDURE IS procedure-name-1 {USING file-name-2 [{THROUGH} {THRU} procedure-name-2]} {OUTPUT PROCEDURE IS procedure-name-3 {GIVING file-name-3. [{THROUGH} {THRU} procedure-name-4]}.

37 Structured COBOL Programming, Stern & Stern, 9th Edition OUTPUT PROCEDURE As indicated, an INPUT PROCEDURE, if used, is processed prior to sorting. –When the SORT verb is encountered, control goes to the INPUT PROCEDURE. –When the INPUT PROCEDURE is complete, the file is then sorted. An OUTPUT PROCEDURE processes all sorted records in the sort file and handles the transfer of these records to the output file.

38 Structured COBOL Programming, Stern & Stern, 9th Edition OUTPUT PROCEDURE Format: RETURN sort-file-name-1 AT END imperative statement-1 [NOT AT END imperative statement-2]* [END-RETURN] * * Valid with COBOL 85 only

39 Structured COBOL Programming, Stern & Stern, 9th Edition OUTPUT PROCEDURE SUMMARY: COBOL 85 1. The OUTPUT PROCEDURE of the SORT should refer to a paragraph-name, but it could refer to a section name. Example: 100-MAIN-MODULE. SORT WORK-FILE USING IN-FILE OUTPUT PROCEDURE 200-AFTER- SORT-MAIN-MODULE STOP RUN. 200-AFTER-SORT-MAIN-MODULE....

40 Structured COBOL Programming, Stern & Stern, 9th Edition OUTPUT PROCEDURE SUMMARY: COBOL 85 2. In the paragraph specified in the OUTPUT PROCEDURE: a. OPEN the output file. b. PERFORM a paragraph that will RETURN and process records from the sort file until there is no more data. c. After all records have been processed, CLOSE the output file. d. When the OUTPUT PROCEDURE paragraph has-been fully executed, control will then return to the SORT.

41 Structured COBOL Programming, Stern & Stern, 9th Edition OUTPUT PROCEDURE SUMMARY: COBOL 85 Example of the OUTPUT Procedure: 200-AFTER-SORT-MAIN-MODULE. OPEN OUTPUT SORTED-FILE PERFORM UNTIL NO-MORE-RECS RETURN WORK-FILE AT END MOVE ‘NO’ TO MORE-RECS NOT AT END PERFORM 300-PROCESS- SORT-RECS END-RETURN END-PERFORM CLOSE SORTED-FILE.

42 Structured COBOL Programming, Stern & Stern, 9th Edition OUTPUT PROCEDURE SUMMARY: COBOL 85 3. At the paragraph that processes the sort records after they have been sorted but before they are output: a. Perform any operations the work or sort records. b. MOVE the work or sort record to the output area. c. WRITE each sort record to the output file. (A WRITE....FROM can be used in place of a MOVE and WRITE.)

43 Structured COBOL Programming, Stern & Stern, 9th Edition OUTPUT PROCEDURE SUMMARY: COBOL 85 Example: 300-PROCESS-SORT-RECS... [Process records in the sort file]. WRITE SORTED-REC FROM WORK-REC.

44 Structured COBOL Programming, Stern & Stern, 9th Edition When To Use INPUT and/or OUTPUT PROCEDURES Sometimes it is more efficient to process data before it is sorted in an INPUT PROCEDURE. Other times it is more efficient to process data after it is sorted in an OUTPUT PROCEDURE. Keep in mind that you must use either an INPUT or an OUTPUT PROCEDURE if the unsorted and sorted files have different- sized fields or have fields in different order.

45 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-1 GIVING {file-name-4 [{THROUGH}{THRU} procedure-name-2]}

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

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

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

49 Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SLIDES END HERE CHAPTER SUMMARY COMES NEXT

50 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

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

52 Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY 2. A program can include an entirely separate routine that processes an unsorted file prior to performing the SORT and/or an entirely separate routine that processes the sorted file after the SORT is executed:

53 Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY 3. An INPUT PROCEDURE that is part of the SORT statement permits processing of the unsorted file just before the sort is performed, yet under the control of the SORT itself: SORT file-name-1... INPUT PROCEDURE procedure-name-1 GIVING file-name-2

54 Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY a. COBOL 85 uses a paragraph-name when specifying an INPUT PROCEDURE. b. With COBOL 85 paragraph-names can be substituted for section-names so there is no need for a GO TO to branch to the end of a section. c. RELEASE sort-rec FROM unsorted-rec is necessary in an INPUT PROCEDURE to make input records available for sorting.

55 Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY 4. An OUTPUT PROCEDURE that is part of the SORT statement permits processing of the sorted work (or sort) file records before they are written to the sorted file: SORT file-name-1... {USING file-name-2} {INPUT PROCEDURE procedure-name-1} OUTPUT PROCEDURE procedure-name-2

56 Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY a. As with the INPUT PROCEDURE, the procedure-name specified must be a section- name for standard COBOL 74 but can be either a section- or paragraph-name with COBOL 85. Using paragraph-names simplifies the coding and eliminates the need for GO TOs. b. An OUTPUT PROCEDURE: (1) Opens the output file. (2) Includes a RETURN sort-file-name AT END... which is like a READ.

57 Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY (3) Processes all records from the sort file before writing them to the sorted- file-name. (4) Uses a RETURN in place of a READ for all inputting of sort-file records. (5) Closes the output sorted-file after all records have been processed. c. Both an INPUT and an OUTPUT PROCEDURE can be used in a program.

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


Download ppt "PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert."

Similar presentations


Ads by Google