Sorting in COBOL M. M. Pickard.

Slides:



Advertisements
Similar presentations
DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.
Advertisements

Chapter 07: Lecture Notes (CSIT 104) 1111 Exploring Microsoft Office Excel 2007 Chapter 7 Data Consolidation, Links, and Formula Auditing.
Repetition control structures
Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert.
14-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Programming Logic and Design Fourth Edition, Introductory
Guide To UNIX Using Linux Third Edition
Understanding the Mainline Logical Flow Through a Program (continued)
Guide To UNIX Using Linux Third Edition
COBOL for the 21 st Century Stern, Stern, Ley Chapter 1 INTRODUCTION TO STRUCTURED PROGRAM DESIGN IN COBOL.
Structured COBOL Programming, Stern & Stern, 9th Edition
Chapter 8 Printing 1. In COBOL you send data to the printer by writing data to a file. In COBOL, the printer is defined as a file, and it is opened, closed,
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
VSAM KSDS and COBOL Department of Computer Science Northern Illinois University August 2005 Some of the illustrations are from VSAM: Access Method Services.
Structured COBOL Programming, Stern & Stern, 9th edition
4-1 Coding Complete COBOL Programs: The PROCEDURE DIVISION Chapter 4.
4-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
4-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
1 Chapter 4. To familiarize you with methods used to 1. Access input and output files 2. Read data from an input file 3. Perform simple move operations.
History COBOL (Common Business Oriented Language) was one of the earliest high-level programming languages. COBOL was first proposed in 1959 by the Conference.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
The DATA DIVISION Chapter 3. COBOL Data Organization Field - group of characters forming a meaningful unit or basic fact –Characters in a name or digits.
Printing on power systems Program/ Command Data Report Layout (Printer File) Job Output Queue *FILE Spooled File.
14- 1 Chapter 14.  To familiarize you with ◦ How files may be sorted ◦ How to process file during SORT procedure  Before it is sorted  After it is.
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
1 The Procedure Division Chapter 4. 2 Main Two Sections File Section –Used to define files and record formats –Field names within records Working Storage.
VSAM Alternate Indexes Department of Computer Science Northern Illinois University August 2005.
Copyright © 2008 Pearson Prentice Hall. All rights reserved Copyright © 2008 Prentice-Hall. All rights reserved. Committed to Shaping the Next.
1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
Artificial Intelligence Lecture No. 26 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
1.  Introduction  The Benefits of the Report Writer Module ◦ For Detail and Summary Printing ◦ For Control Break Processing ◦ For Printing Headings.
Control Break Processing
COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Analysis of SAMPLE1.CBL Please check speaker notes for additional information!
Chapter 4 PROCEDURE DIVISION. Paragraphs PROCEDURE DIVISION divided into paragraphs Each is independent module or routine Made up of series of instructions.
VSAM ESDS and RRDS Department of Computer Science Northern Illinois University September 2005 Some of the illustrations are from VSAM: Access Method Services.
Random update Please use speaker notes for additional information!
SQL Triggers, Functions & Stored Procedures Programming Operations.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
IST 220 – Intro to Databases
RPG Programming with Printer Files
THE SORT STATEMENT for files (chp. 14)
Lesson 23 Managing and Reporting Database Information
Exam 3 Review.
RPG Programming with Printer Files
Designing and Debugging Batch and Interactive COBOL Programs
Topics Introduction to File Input and Output
Any Questions?.
Chapter 3 The DATA DIVISION.
Chapter 9 Structuring System Requirements: Logic Modeling
Programming Logic and Design Fourth Edition, Comprehensive
Structured Program Design
Chapter 9 Lesson 2 Notes.
Designing and Writing Control-Break Programs
Minor, Intermediate and Major Breaks
Chapter 14 Sorting and Merging.
Scope and System Time/Date
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
SCOPE TERMINATORS Definition:
Programming in COBOL-85 For IBM Mainframe System 390
Agenda Collating sequence / Sorting data
CHAPTER 17 The Report Writer Module
Lesson 24 Managing and Reporting Database Information
Access: Reports Participation Project
Chapter 9 Structuring System Requirements: Logic Modeling
Topics Introduction to File Input and Output
IT Solutions for Administrators - Databases
Presentation transcript:

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.

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 dataname1 DESCENDING USING filename2 INPUT 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 all the 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.