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.

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

Benchmark Series Microsoft Access 2010 Level 1
2-1 Chapter 2.  Coding Requirements of IDENTIFICATION DIVISION  Sections of ENVIRONMENT DIVISION  Assigning Files to Devices in ENVIRONMENT DIVISION.
3-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emertius)
The IDENTIFICATION and ENVIRONMENT DIVISIONS Chapter 2.
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)
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,
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
VSAM KSDS and COBOL Department of Computer Science Northern Illinois University August 2005 Some of the illustrations are from VSAM: Access Method Services.
15-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)
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)
2-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.
Chapter To familiarize you with  Why COBOL is a popular business-oriented language.  Programming practices and techniques  History of COBOL.
3-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emertius)
PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert.
12- 1 Chapter 12.  Single-Level OCCURS Clauses  Processing Data Stored in Array  Using OCCURS Clause for Table Handling  Use of SEARCH Statement 
History COBOL (Common Business Oriented Language) was one of the earliest high-level programming languages. COBOL was first proposed in 1959 by the Conference.
PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert.
1 Chapter 14 - Sorting System Concepts –Sort key Major Key (Primary) Minor Key (Secondary) –Sort sequence Ascending - Low to high Descending – High to.
Lecture 31 Numeric Edited Alphabetic (A) AlphaNumeric (X) Numeric (9, V, S) Numeric Edited (9, Z, comma, decimal point, minus sign) –Z = zero suppressed.
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.
3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.
3-1 The DATA DIVISION Chapter Chapter Objectives To familiarize you with Systems design considerations Ways in which data is organized Rules for.
1 Interactive vs Batch Programs Cobol suited for developing both types of programs Interactive programs Accept input data from keyboard Input data processed.
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.
Indexed and Relative File Processing
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.
13-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)
Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout.
11- 1 Chapter 11.  Avoiding Logic Errors by Validating Input  What to Do If Input Errors Occur  Global Considerations in COBOL  When Data Should Be.
12- 1 Chapter 12.  Single-Level OCCURS Clauses  Processing Data Stored in Array  Using OCCURS Clause for Table Handling  Use of SEARCH Statement 
Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER 2 Cobol Language Fundamentals.
1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
1 Lesson 18 Managing and Reporting Database Information Computer Literacy BASICS: A Comprehensive Guide to IC 3, 3 rd Edition Morrison / Wells.
13-1 Sequential File Processing Chapter Chapter Contents Overview of Sequential File Processing Sequential File Updating - Creating a New Master.
PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert.
2-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)
PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert.
1.  Introduction  The Benefits of the Report Writer Module ◦ For Detail and Summary Printing ◦ For Control Break Processing ◦ For Printing Headings.
13- 1 Chapter 13.  Overview of Sequential File Processing  Sequential File Updating - Creating a New Master File  Validity Checking in Update Procedures.
COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
11- 1 Chapter 11.  Avoiding Logic Errors by Validating Input  What to Do If Input Errors Occur  Global Considerations in COBOL  When Data Should Be.
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.
Week 2/3 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data.
Aggregator Stage : Definition : Aggregator classifies data rows from a single input link into groups and calculates totals or other aggregate functions.
16- 1 Chapter 16.  To familiarize you with  COPY statement for copying parts of a program stored in a library  CALL statement for executing called.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Sorting in COBOL M. M. Pickard.
THE SORT STATEMENT for files (chp. 14)
Designing and Debugging Batch and Interactive COBOL Programs
Topics Introduction to File Input and Output
Chapter 3 The DATA DIVISION.
An Introduction to Structured Program Design in COBOL
Structured COBOL Programming
Chapter 14 Sorting and Merging.
Iteration: Beyond the Basic PERFORM
Programming in COBOL-85 For IBM Mainframe System 390
Agenda Collating sequence / Sorting data
CHAPTER 17 The Report Writer Module
Topics Introduction to File Input and Output
Decision Making Using the IF and EVALUATE Statements
Presentation transcript:

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 sorted ◦ How to merge files 14- 2

 SORT Feature  Processing Data Before and/or After Sorting  MERGE Statement 14- 3

 Sort then sequential batch processing performed  Sorting ◦ You could use a sort utility separate from COBOL program ◦ Use COBOL's SORT verb in program ◦ Write your own 14- 4

SORT file-name-1 ONASCENDING KEY data-name-1 … DESCENDING USING file-name-2 GIVINGfile-name Simplified Format

 To specify sequence for key field ◦ ASCENDING: From lowest to highest ◦ DESCENDING: From highest to lowest  Sort key fields may be numeric or nonnumeric  Alphanumeric fields sorted according to collating sequence (ASCII or EBCDIC) used by computer  Actually looks at the ASCII or EBCDIC value for sorting 14- 6

 Can sequence records with more than one key field  Sort payroll file in ascending alphabetic sequence by name, within each level, for each office ◦ Office number - major sort field ◦ Level number - intermediate sort field ◦ Name - minor sort field 14- 7

 For Office 1, desired sequence is Office-No Level-NoName 11ADAMS, J. R. 11BROCK, P. T. 11LEE, S. 12ARTHUR, Q. C. 12SHAH, J. 13RAMIREZ, A. P. Easy you all know this 14- 8

 Sorts records into ascending name sequence within level within office Sort Sort-File On Ascending Key Office-No On Ascending Key Level-No On Ascending Key Name Using Payroll-File-In Giving Sort-Payroll-File-Out 14- 9

 Choose either ASCENDING or DESCENDING sequence for each key  If all key fields to be sorted in same sequence, can condense coding Sort Sort-File On Ascending KeyMajor-Key Intermediate-Key Minor-Key Example

 Assume records to be sorted in descending order by salary  If both 9th and 24th records in input file have salary of 30000, which appears first in sort file?  Can specify that records with same value for key field be placed in sort file in same order that they appear in original input file

Sort Sort-File On Descending Key Srt-Salary With Duplicates In Order Using Unsorted-File-In Giving Sorted-File-Out  DUPLICATES clause ensures that 9th record appears before 24th in Sort-File if both have same Salary value  I believe without this its “Unpredictable”

 Input file: File of unsorted input records  Work or sort file: File used to store records temporarily during sorting process  Output file: File of sorted output records

 Use 3 files  All defined using standard SELECT … ASSIGN entries  All must have same record format  All are opened and closed automatically by SORT

 Input and output file described with FD entries  Sort work file ◦ Described with SD entry (Sort Descriptor) ◦ Temporary file used only during sorting but not saved ◦ Sort key fields must be described as part of sort record format

Data Division. File Section. FD Unsorted-File-In. 01Unsorted-Rec-In. 05Name-InPic X(20). 05Salary-InPic 9(6)

SDSort-File. 01Sort-Rec. 05Srt-NamePic X(20). 05Srt-SalaryPic 9(6). FD Sorted-File-Out. 01Sorted-Rec-Out. 05Name-OutPic X(20). 05Salary-OutPic 9(6)

 Opens all three files  Moves all records from Unsorted-File-In to Sort-File  Sorts records in Sort-File in descending sequence by Srt-Salary  Moves all records from Sort-File to Unsorted- File-Out  Closes all three files

 Use in place of USING clause to process data from input file prior to sorting  For example only records with Salary-In < may need to be sorted  Use Input Procedure to process and select desired records before sorting

Sort Sort-File On Descending Key Srt-Salary Input Procedure Select-Records Giving Sorted-File-Out  Select-Records is name of paragraph written by programmer to process records before sorting

Select-Records paragraph must  Open input file (Unsorted-File-In)  Perform processing of input records until there is no more data  Close input file

 For each input record, if Salary-In < ◦ Move input data to sort record ◦ RELEASE record to sort file  When INPUT PROCEDURE paragraph is completed, control returns to SORT

RELEASE sort-record-name-1 [FROM identifier-1]  To write a record to the sort file  Like WRITE but used to output sort records  Example p Format

May be used to  Validate data in input records  Process only records that meet certain criteria  Eliminate records with blank fields  Remove unneeded fields from input records  Count input records

 With GIVING option, records in sort file automatically written to output file after sorting  Use OUTPUT PROCEDURE to process sorted records prior to, or instead of, placing them in output file  Ex. Page 629/

SORT file-name-1 ONASCENDING KEY data-name-1 … … DESCENDING INPUT PROCEDURE IS procedure-name-1 USING file-name-2 … OUTPUT PROCEDURE IS procedure-name-3 GIVING file-name-3 …

 If INPUT PROCEDURE used ◦ SORT transfers control to paragraph or section named in INPUT PROCEDURE ◦ When complete, sort file is sorted  If OUTPUT PROCEDURE used ◦ SORT transfers control to paragraph or section named in OUTPUT PROCEDURE ◦ Processes all sorted records in sort file and handles transfer of records to output file  Why do this instead of a separate procedure? ◦ Saves Opening, Reading, Closing operation

 In INPUT PROCEDURE, records RELEASEd to sort file  In OUTPUT PROCEDURE, records RETURNed from sort file

RETURN sort-file-name-1 AT END imperative statement-1 [ NOT AT END imperative statement-2] [END-RETURN]  To retrieve records from the sort file  Similar to READ  Use RETURN to access the records (doesn’t make sense) Format

Paragraph (or section) must  Open output file  Perform paragraph to RETURN and process records from sort file until there is no more data  Close output file  When OUTPUT PROCEDURE finished, control returns to SORT

After records sorted but before they are created as output  Perform any operations on sort records  MOVE sort record to output area  WRITE each sort record to output file

 Both INPUT and OUTPUT PROCEDUREs can be used in same program  If used, programmer must open/close the input or output file  SD (sort descriptor) file and files specified with USING or GIVING are automatically opened and closed

 More efficient to use INPUT PROCEDURE if many records in input file can be eliminated before sort  Use OUTPUT PROCEDURE if records require further processing after sort  Must use procedure if input or output file and sorted file have different-sized fields or fields in different order

Option:INPUT PROCEDURE GIVING Result:  Processes unsorted input records before they are sorted  Write records to sort file with RELEASE  After INPUT PROCEDURE completed, records are sorted

Option:USING OUTPUT PROCEDURE Result:  Processes records after they have been sorted but before they are written to output file  Read records from sort file with RETURN

Option:INPUT PROCEDURE OUTPUT PROCEDURE Result:  Using both in the one command  Processes data both before and after it is sorted

 To combine two or more files into one  Files to be merged must each be in sequence by key field ◦ So you may need to sort first  Format similar to SORT, rules for clauses are same

MERGE file-name-1 ON ASCENDING KEY data-name-1 … … DESCENDING USING file-name-2 file-name-3 … OUTPUT PROCEDURE IS procedure-name-1 GIVING file-name-4 …  To combine two or more files into one

 File-name-1 is work file designated as an SD  Keys specified are defined within SD  Data-name-1 is major key, may be followed by intermediate and minor keys  USING clause names file to be merged ◦ At least two must be included

 Records may be processed after merging with OUTPUT PROCEDURE, but not before  Automatically handles opening, closing, and input/output associated with files

 Suppose two separate files of employees are to be combined into one  Both input files and the resulting output file contain 80 characters with an Emp-No in the first nine positions  File definitions and MERGE instruction follow

Data Division. File Section. FDEmp-File-1. 01Emp-Rec-1Pic X(80). FDEmp-File-2. 01Emp-Rec-2Pic X(80)

SDMerge-File. 01Merge-Rec. 05Mrg-Emp-NoPic X(9). 05Rest-of-RecPic X(71). FDOut-Emp-File. 01Out-Emp-RecPic X(80)

Procedure Division. 100-Main-Module. Merge Merge-File On Ascending Key Mrg-Emp-No Using Emp-File-1, Emp-File-2 Giving Out-Emp-File Stop Run

14- 45

 SORT used for sorting records in either ascending or descending order  SORT uses work or sort file described with an SD  Key fields to be sorted are data-names defined within SD or sort file  Files may be sorted using more than one key field

 Routines separate from SORT may be used to ◦ Process unsorted file prior to SORT ◦ Process sorted file after SORT  Procedures that are part of SORT permit processing ◦ Just before sort performed (INPUT PROCEDURE) ◦ After sort finished but before writing records to sorted file (OUTPUT PROCEDURE)

 RELEASE statement used in INPUT PROCEDURE to make input records available for sorting  RETURN statement used in OUTPUT PROCEDURE to read records from sort file ◦ Think of it as a READ statement

 MERGE statement used to merge two or more files into one