18
Module 7 Sorting

Module 7 Sorting. The StudentFile is a sequential file sequenced upon ascending StudentId. How to sort the file by StudentId? DATA DIVISION. FILE

Embed Size (px)

Citation preview

Page 1: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

Module 7Sorting

Page 2: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

The StudentFile is a sequential file sequenced The StudentFile is a sequential file sequenced upon ascending StudentId.upon ascending StudentId.

How to sort the file by StudentId?How to sort the file by StudentId?

DATA DIVISION.DATA DIVISION.FILE SECTION.FILE SECTION.FD StudentFile.FD StudentFile.01 StudentDetails.01 StudentDetails. 02 StudentId PIC 9(7). 02 StudentId PIC 9(7). 02 StudentName. 02 StudentName. 03 FirstName PIC X(5). 03 FirstName PIC X(5). 03 LastName PIC X(5). 03 LastName PIC X(5). 02 DateOfBirth. 02 DateOfBirth. 03 YOBirth PIC 9(2). 03 YOBirth PIC 9(2). 03 MOBirth PIC 9(2). 03 MOBirth PIC 9(2). 03 DOBirth PIC 9(2). 03 DOBirth PIC 9(2). 02 CourseCode PIC X(7). 02 CourseCode PIC X(7). 02 Gender PIC X. 02 Gender PIC X.

Page 3: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

Simplified Sort Syntax.Simplified Sort Syntax.

The The WorkFileNameWorkFileName identifies a temporary work file that identifies a temporary work file that the SORT process uses for the sort. It is defined in the the SORT process uses for the sort. It is defined in the FILE SECTIONFILE SECTION using an using an SDSD entry. entry.

Each Each SortKeyIdentifierSortKeyIdentifier identifies a field in the record of identifies a field in the record of the work file upon which the file will be sequenced.the work file upon which the file will be sequenced.

When more than one When more than one SortKeyIdentifier SortKeyIdentifier is specified, the is specified, the keys decrease in significance from left to right (leftmost keys decrease in significance from left to right (leftmost key is most significant, rightmost is least significant).key is most significant, rightmost is least significant).

InFileName InFileName and and OutFileNameOutFileName, are the names of the input , are the names of the input and output files. These files are automatically opened by and output files. These files are automatically opened by the SORT. When the SORT executes they must the SORT. When the SORT executes they must notnot be be already open. already open.

Page 4: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

FD SalesFile.01 SalesRec. 02 FILLER PIC X(10).

SD WorkFileWorkFile.01 WorkRec. 02 WSalesmanNumWSalesmanNum PIC 9(5). 02 FILLER PIC X(5).

FD SortedSalesFile.01 SortedSalesRec. 02 SalesmanNum PIC 9(5). 02 ItemType PIC X. 02 QtySold PIC 9(4).

PROCEDURE DIVISION.Begin. SORT WorkFileWorkFile ON ASCENDING KEY WSalesmanNumWSalesmanNum USING SalesFile GIVING SortedSalesFile.

OPEN INPUT SortedSalesFile.

Sort Example.Sort Example.

Page 5: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT WorkFile ASSIGN TO "WORK.TMP".

SD WorkFile.01 WorkRecord. 02 ProvinceCode PIC 9. 02 SalesmanCode PIC 9(5). 02 FILLER PIC X(19).

PROCEDURE DIVISION.Begin. SORT WorkFile ON ASCENDING KEY ProvinceCode DESCENDING KEY SalesmanCode USING UnsortedSales GIVING SortedSales.

OPEN INPUT SortedSales.

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT WorkFile ASSIGN TO "WORK.TMP".

SD WorkFile.01 WorkRecord. 02 ProvinceCode PIC 9. 02 SalesmanCode PIC 9(5). 02 FILLER PIC X(19).

PROCEDURE DIVISION.Begin. SORT WorkFile ON ASCENDING KEY ProvinceCode DESCENDING KEY SalesmanCode USING UnsortedSales GIVING SortedSales.

OPEN INPUT SortedSales.

Page 6: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

SORTSORTProcessProcess

WorkFileWorkFile

How the SORT works.How the SORT works.

SORT WorkFile ON ASCENDING KEY WSalesmanNumSORT WorkFile ON ASCENDING KEY WSalesmanNum USING SalesFile USING SalesFile GIVING SortedSalesFile. GIVING SortedSalesFile.

SalesFileSalesFile SortedSalesFileSortedSalesFile

UnsortedRecords

SortedRecords

Page 7: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

SORTSORTProcessProcess

How the INPUT PROCEDURE works.How the INPUT PROCEDURE works.

SORT WorkFile ON ASCENDING KEY WSalesmanNumSORT WorkFile ON ASCENDING KEY WSalesmanNum INPUT PROCEDURE IS SelectHatSales INPUT PROCEDURE IS SelectHatSales GIVING SortedSalesFile. GIVING SortedSalesFile.

WorkFileWorkFile

SalesFileSalesFile SortedSalesFileSortedSalesFile

SortedRecords

SelectHatSalesSelectHatSales

UnsortedHat

Records

UnsortedRecords

Page 8: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

OPEN INPUT InFileNameOPEN INPUT InFileNameREAD InFileName RECORDREAD InFileName RECORDPERFORM UNTIL ConditionPERFORM UNTIL Condition RELEASE SDWorkRecRELEASE SDWorkRec READ InFileName RECORD READ InFileName RECORDEND-PERFORMEND-PERFORMCLOSE InFileCLOSE InFile

OPEN INPUT InFileNameOPEN INPUT InFileNameREAD InFileName RECORDREAD InFileName RECORDPERFORM UNTIL ConditionPERFORM UNTIL Condition RELEASE SDWorkRecRELEASE SDWorkRec READ InFileName RECORD READ InFileName RECORDEND-PERFORMEND-PERFORMCLOSE InFileCLOSE InFile

INPUT PROCEDURE TemplateINPUT PROCEDURE Template

Page 9: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

FD SalesFile.01 SalesRec. 88 EndOfSales VALUE HIGH-VALUES. 02 FILLER PIC 9(5). 02 FILLER PIC X. 88 HatRecord VALUE "H". 02 FILLER PIC X(4).SD WorkFile.01 WorkRec. 02 WSalesmanNum PIC 9(5). 02 FILLER PIC X(5).FD SortedSalesFile.01 SortedSalesRec. 02 SalesmanNum PIC 9(5). 02 ItemType PIC X. 02 QtySold PIC 9(4).PROCEDURE DIVISION.Begin. SORT WorkFile ON ASCENDING KEY WSalesmanNum INPUT PROCEDURE IS SelectHatSalesSelectHatSales GIVING SortedSalesFile.

FD SalesFile.01 SalesRec. 88 EndOfSales VALUE HIGH-VALUES. 02 FILLER PIC 9(5). 02 FILLER PIC X. 88 HatRecord VALUE "H". 02 FILLER PIC X(4).SD WorkFile.01 WorkRec. 02 WSalesmanNum PIC 9(5). 02 FILLER PIC X(5).FD SortedSalesFile.01 SortedSalesRec. 02 SalesmanNum PIC 9(5). 02 ItemType PIC X. 02 QtySold PIC 9(4).PROCEDURE DIVISION.Begin. SORT WorkFile ON ASCENDING KEY WSalesmanNum INPUT PROCEDURE IS SelectHatSalesSelectHatSales GIVING SortedSalesFile.

INPUT PROCEDURE - ExampleINPUT PROCEDURE - Example

Page 10: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

New Version New Version

SelectHatSales. OPEN INPUT SalesFile

READ SalesFile AT END SET EndOfSales TO TRUE END-READ PERFORM UNTIL EndOfSales IF HatRecord RELEASE WorkRec FROM SalesRec END-IF READ SalesFile AT END SET EndOfSales TO TRUE END-READ END-PERFORM

CLOSE SalesFile.

SelectHatSales. OPEN INPUT SalesFile

READ SalesFile AT END SET EndOfSales TO TRUE END-READ PERFORM UNTIL EndOfSales IF HatRecord RELEASE WorkRec FROM SalesRec END-IF READ SalesFile AT END SET EndOfSales TO TRUE END-READ END-PERFORM

CLOSE SalesFile.

Page 11: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT WorkFile ASSIGN TO "WORK.TMP".

SD WorkFile.01 WorkRecord. 88 EndOfWorkFile VALUE HIGH-VALUES. 02 ProvinceCode PIC 9. 88 ProvinceIsUlster VALUE 4. 02 SalesmanCode PIC 9(5). 02 FILLER PIC X(19).FD UnsortedSales.01 FILLER PIC X(25).FD SortedSales.01 SortedRec. 88 EndOfSalesFile VALUE HIGH-VALUES. 02 ProvinceCode PIC 9. 02 SalesmanCode PIC 9(5). 02 ItemCode PIC 9(7). 02 ItemCost PIC 9(3)V99. 02 QtySold PIC 9(7).

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT WorkFile ASSIGN TO "WORK.TMP".

SD WorkFile.01 WorkRecord. 88 EndOfWorkFile VALUE HIGH-VALUES. 02 ProvinceCode PIC 9. 88 ProvinceIsUlster VALUE 4. 02 SalesmanCode PIC 9(5). 02 FILLER PIC X(19).FD UnsortedSales.01 FILLER PIC X(25).FD SortedSales.01 SortedRec. 88 EndOfSalesFile VALUE HIGH-VALUES. 02 ProvinceCode PIC 9. 02 SalesmanCode PIC 9(5). 02 ItemCode PIC 9(7). 02 ItemCost PIC 9(3)V99. 02 QtySold PIC 9(7).

Page 12: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

PROCEDURE DIVISION.Begin. SORT WorkFile ON ASCENDING KEY ProvinceCode SalesmanCode INPUT PROCEDURE IS SelectUlsterRecs GIVING SortedSales.

OPEN INPUT SortedSales.

SelectUlsterRecs. OPEN INPUT UnsortedSales READ UnsortedSales INTO WorkRec AT END SET EndOfSalesFile TO TRUE END-READ

PERFORM UNTIL EndOfSalesFile IF ProvinceIsUlster RELEASE WorkRec END-IF READ UnsortedSales INTO WorkRec AT END SET EndOfSalesFile TO TRUE END-READ END-PERFORM

CLOSE UnsortedSales

PROCEDURE DIVISION.Begin. SORT WorkFile ON ASCENDING KEY ProvinceCode SalesmanCode INPUT PROCEDURE IS SelectUlsterRecs GIVING SortedSales.

OPEN INPUT SortedSales.

SelectUlsterRecs. OPEN INPUT UnsortedSales READ UnsortedSales INTO WorkRec AT END SET EndOfSalesFile TO TRUE END-READ

PERFORM UNTIL EndOfSalesFile IF ProvinceIsUlster RELEASE WorkRec END-IF READ UnsortedSales INTO WorkRec AT END SET EndOfSalesFile TO TRUE END-READ END-PERFORM

CLOSE UnsortedSales

Page 13: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

SummariseSalesSummariseSales

SORTSORTProcessProcess

WorkFileWorkFile

How the OUTPUT PROCEDURE works.How the OUTPUT PROCEDURE works.

SORT WorkFile ON ASCENDING KEY WSalesmanNumSORT WorkFile ON ASCENDING KEY WSalesmanNum USING SalesFile USING SalesFile OUTPUT PROCEDURE IS SummariseSales. OUTPUT PROCEDURE IS SummariseSales.

SalesFileSalesFile SalesSummaryFileSalesSummaryFile

UnsortedRecords

SalesmanSummary

RecordSortedRecords

Page 14: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

OPEN OUTPUT OutFileOPEN OUTPUT OutFileRETURN SDWorkFile RECORDRETURN SDWorkFile RECORDPERFORM UNTIL ConditionPERFORM UNTIL Condition WRITE OutRec WRITE OutRec RETURN SDWorkFile RECORD RETURN SDWorkFile RECORDEND-PERFORMEND-PERFORMCLOSE OutFileCLOSE OutFile

OUTPUT PROCEDURE TemplateOUTPUT PROCEDURE Template

Page 15: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

FD SalesFile.01 SalesRec PIC X(10).SD WorkFile.01 WorkRec. 88 EndOfWorkFile VALUE HIGH-VALUES. 02 WSalesmanNum PIC 9(5). 02 FILLER PIC X. 02 WQtySold PIC X(4).FD SalesSummaryFile.01 SummaryRec. 02 SalesmanNum PIC 9(5). 02 TotalQtySold PIC 9(6).PROCEDURE DIVISION.Begin. SORT WorkFile ON ASCENDING KEY WSalesmanNum USING SalesFile OUTPUT PROCEDURE IS SummariseSalesSummariseSales. OPEN INPUT SalesSummaryFile. PERFORM PrintSummaryReport.

FD SalesFile.01 SalesRec PIC X(10).SD WorkFile.01 WorkRec. 88 EndOfWorkFile VALUE HIGH-VALUES. 02 WSalesmanNum PIC 9(5). 02 FILLER PIC X. 02 WQtySold PIC X(4).FD SalesSummaryFile.01 SummaryRec. 02 SalesmanNum PIC 9(5). 02 TotalQtySold PIC 9(6).PROCEDURE DIVISION.Begin. SORT WorkFile ON ASCENDING KEY WSalesmanNum USING SalesFile OUTPUT PROCEDURE IS SummariseSalesSummariseSales. OPEN INPUT SalesSummaryFile. PERFORM PrintSummaryReport.

Output PROCEDURE - ExampleOutput PROCEDURE - Example

Page 16: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

SummariseSales. OPEN OUTPUT SalesSummaryFile RETURN WorkFile AT END SET EndOfWorkFile TO TRUE END-RETURN PERFORM UNTIL EndOfWorkFile MOVE WSalesmanNum TO SalesmanNum MOVE ZEROS TO TotalQtySold PERFORM UNTIL WSalesManNum NOT = SalesmanNum OR EndOfWorkFile ADD WQtySold TO TotalQtySold RETURN WorkFile AT END SET EndOfWorkFile TO TRUE END-RETURN END-PERFORM WRITE SummaryRec END-PERFORM CLOSE SalesSummaryFile.

SummariseSales. OPEN OUTPUT SalesSummaryFile RETURN WorkFile AT END SET EndOfWorkFile TO TRUE END-RETURN PERFORM UNTIL EndOfWorkFile MOVE WSalesmanNum TO SalesmanNum MOVE ZEROS TO TotalQtySold PERFORM UNTIL WSalesManNum NOT = SalesmanNum OR EndOfWorkFile ADD WQtySold TO TotalQtySold RETURN WorkFile AT END SET EndOfWorkFile TO TRUE END-RETURN END-PERFORM WRITE SummaryRec END-PERFORM CLOSE SalesSummaryFile.

Page 17: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

RELEASE and RETURN SyntaxRELEASE and RETURN Syntax

WriteWrite

ReadRead

Page 18: Module 7 Sorting.  The StudentFile is a sequential file sequenced upon ascending StudentId.  How to sort the file by StudentId? DATA DIVISION. FILE

Feeding the SORT from the keyboard.Feeding the SORT from the keyboard.

SORT WorkFile ON ASCENDING KEY WStudentIdSORT WorkFile ON ASCENDING KEY WStudentId INPUT PROCEDURE IS GetStudentDetails INPUT PROCEDURE IS GetStudentDetails GIVING StudentFile. GIVING StudentFile.

WorkFileWorkFile

StudentFileStudentFile

SortedStudentRecords

SORTSORTProcessProcess

GetStudentDetailsGetStudentDetails

UnsortedStudentRecords

8965125COUGHLAN