14- 1 chapter 14. to familiarize you with ◦ how files may be sorted ◦ how to process file...
TRANSCRIPT
![Page 1: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/1.jpg)
14-1
Chapter 14
![Page 2: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/2.jpg)
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
![Page 3: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/3.jpg)
SORT Feature Processing Data Before and/or After Sorting MERGE Statement
14-3
![Page 4: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/4.jpg)
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
![Page 5: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/5.jpg)
SORT file-name-1 ON ASCENDING KEY data-name-1 …
DESCENDING
USING file-name-2 GIVING file-name-3
14-5
Simplified Format
![Page 6: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/6.jpg)
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
![Page 7: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/7.jpg)
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
![Page 8: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/8.jpg)
For Office 1, desired sequence isOffice-No Level-No Name
1 1 ADAMS, J. R.1 1 BROCK, P. T.1 1 LEE, S.1 2 ARTHUR, Q. C.1 2 SHAH, J.1 3 RAMIREZ, A. P.
Easy you all know this
14-8
![Page 9: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/9.jpg)
Sorts records into ascending name sequence within level within office
Sort Sort-FileOn Ascending Key Office-NoOn Ascending Key Level-NoOn Ascending Key Name
Using Payroll-File-InGiving Sort-Payroll-File-Out
14-9
![Page 10: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/10.jpg)
Choose either ASCENDING or DESCENDING sequence for each key
If all key fields to be sorted in same sequence, can condense coding
Sort Sort-FileOn Ascending Key Major-Key
Intermediate-Key
Minor-Key
...
14-10
Example
![Page 11: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/11.jpg)
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
14-11
![Page 12: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/12.jpg)
Sort Sort-FileOn Descending Key Srt-Salary
With Duplicates In OrderUsing Unsorted-File-InGiving 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”
14-12
![Page 13: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/13.jpg)
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
14-13
![Page 14: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/14.jpg)
Use 3 files All defined using standard SELECT …
ASSIGN entries All must have same record format All are opened and closed automatically by
SORT
14-14
![Page 15: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/15.jpg)
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
14-15
![Page 16: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/16.jpg)
Data Division.File Section.FD Unsorted-File-In.01 Unsorted-Rec-In.
05 Name-In Pic X(20).05 Salary-In Pic 9(6).
14-16
![Page 17: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/17.jpg)
SD Sort-File.01 Sort-Rec.
05 Srt-Name Pic X(20).05 Srt-Salary Pic 9(6).
FD Sorted-File-Out.01 Sorted-Rec-Out.
05 Name-Out Pic X(20).05 Salary-Out Pic 9(6).
14-17
![Page 18: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/18.jpg)
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
14-18
![Page 19: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/19.jpg)
Use in place of USING clause to process data from input file prior to sorting
For example only records with Salary-In < 75000 may need to be sorted
Use Input Procedure to process and select desired records before sorting
14-19
![Page 20: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/20.jpg)
Sort Sort-FileOn 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
14-20
![Page 21: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/21.jpg)
Select-Records paragraph must Open input file (Unsorted-File-In) Perform processing of input records until
there is no more data Close input file
14-21
![Page 22: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/22.jpg)
For each input record, if Salary-In < 75000◦ Move input data to sort record◦ RELEASE record to sort file
When INPUT PROCEDURE paragraph is completed, control returns to SORT
14-22
![Page 23: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/23.jpg)
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. 623
14-23
Format
![Page 24: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/24.jpg)
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
14-24
![Page 25: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/25.jpg)
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/630
14-25
![Page 26: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/26.jpg)
SORT file-name-1 ON ASCENDING 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 …
14-26
![Page 27: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/27.jpg)
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
14-27
![Page 28: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/28.jpg)
In INPUT PROCEDURE, records RELEASEd to sort file
In OUTPUT PROCEDURE, records RETURNed from sort file
14-28
![Page 29: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/29.jpg)
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)
14-29
Format
![Page 30: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/30.jpg)
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
14-30
![Page 31: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/31.jpg)
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
14-31
![Page 32: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/32.jpg)
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
14-32
![Page 33: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/33.jpg)
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
14-33
![Page 34: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/34.jpg)
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
14-37
![Page 35: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/35.jpg)
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
14-38
![Page 36: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/36.jpg)
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
14-39
![Page 37: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/37.jpg)
Records may be processed after merging with OUTPUT PROCEDURE, but not before
Automatically handles opening, closing, and input/output associated with files
14-40
![Page 38: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/38.jpg)
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
14-41
![Page 39: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/39.jpg)
Data Division.File Section.FD Emp-File-1.01 Emp-Rec-1 Pic X(80).
FD Emp-File-2.01 Emp-Rec-2 Pic X(80).
14-42
![Page 40: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/40.jpg)
SD Merge-File.• 01 Merge-Rec.• 05 Mrg-Emp-No Pic X(9).
• 05 Rest-of-Rec Pic X(71).
FD Out-Emp-File.01 Out-Emp-Rec Pic X(80).
14-43
![Page 41: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/41.jpg)
Procedure Division.100-Main-Module. Merge Merge-File
On Ascending Key Mrg-Emp-NoUsing Emp-File-1, Emp-File-2Giving Out-Emp-File
Stop Run.
14-44
![Page 42: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/42.jpg)
14-45
![Page 43: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/43.jpg)
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
14-46
![Page 44: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/44.jpg)
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)
14-47
![Page 45: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/45.jpg)
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
14-48
![Page 46: 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](https://reader035.vdocument.in/reader035/viewer/2022062304/56649ee75503460f94bf8e18/html5/thumbnails/46.jpg)
MERGE statement used to merge two or more files into one
14-49