sams_teach_yourself_cobol_in_24_hours_-_hour_17_sorting

Sams Teach Yourself COBOL in 24 Hours - Hour 17 Sorting

Return to Teach Yourself COBOL in 24 Hours, COBOL bibliography, COBOL, COBOL DevOps, Awesome COBOL, Awesome IBM Mainframe, IBM Mainframe development, IBM Mainframe bibliography, Fortran

“ (TYCb24H 1998)

Hour 17 Sorting

One of the tasks frequently required in business is data file sorting. Reports are created from data files in different sequences, allowing business professionals to analyze information and make sound business decisions. Sorting data frequently occurs within the normal business process. In this hour, various aspects of sorting are covered, including topics such as

• The Sort Work File

• The Sort Key

• The Using and Giving clauses

• Preprocessing Input, using the Input Procedure

• Post-Sort processing, using the Output Procedure

Sometimes data is sorted prior to being loaded to an Indexed file. As you found out in Hour 14, “Indexed Files,” you cannot Write records to an Indexed file Open for Output with Sequential access unless the data is in primary Key sequence. Sorting the data file can help in the quick creation of the Indexed file.

Data might also be sorted for update purposes. When updating a master file, you might collect transactional data from many sources. This data is then sorted in the same sequence as the master file that the data is updating. This process creates an orderly and fast update sequence. Within the transactions, you might want certain transactions to be applied before others. For example, you might want all ordering transactions for an item to be processed before any sales transactions. Sorting can ensure that the input data is in the proper sequence.

Sorting a File COBOL provides very easy and efficient sorting methods. You need not write your own program to sort the data in the desired sequence. With a simple statement, COBOL allows you to sort a data file. You may even sort the data file in place. That is, you can take a file, sort it, and not create a separate output file.

Each sort in your program uses a Sort Work File, which contains the records as they are sorted by the system. You must declare these files in your program with a Select statement, like any other file. File organization and access modes are not specified for the file. However, you must select a unique filename.

Image

In Fujitsu COBOL, Sort Work Files are not assigned to a physical file. The physical filename in the Sort Work File Assign statement is for internal purposes only. When the actual Sort Work File is created, Fujitsu creates it in the directory defined by your TEMP= environment variable. Other COBOL compilers may require you to specify a physical filename for the Sort Work File.

Your Sort Work File Select might look something like this:

000010 Select Sort-Work Assign to Symbolic-Sort-Name.

Symbolic-Sort-Name is not defined in your program. Fujitsu uses it to keep track of the file internally, but assigns its own temporary Sort Work File with a system-determined unique name.

In addition to the Select statement, a special File Section entry is required under the Data Division. This entry is the Sort Description, or SD. The SD is coded in exactly the same manner as an FD, but identifies the file as a Sort Work File to the system. A typical SD is coded as follows:

000020 SD Sort-Work. 000021 01 Sort-Record. 000022 03 Sort-Field-1 Pic X(20). 000023 03 Sort-Field-2 Pic X(20). 000024 03 Filler Pic X(20). As you can see, there is no special difference between an SD and an FD. The SD simply refers to the Sort Work File.

The Using and Giving Clauses The simplest sort reads an input file, sorts the records, and creates an output file. The records in the three files have the same record layout. The following program sorts the Dealer.TXT file that you have used in previous examples. This Line Sequential file is now in dealer-number sequence, but the sort puts it in last name, first name, and middle name sequence. This sort sorts in place, meaning that it does not create a new file from Dealer.TXT, but rather replaces Dealer.TXT with a version of itself, sorted in a different sequence.

This work is accomplished by the Sort statement, with a Using and Giving clause. The Sort statement specifies the name of the Sort Work File, which is the file that is actually being sorted, and the data fields that are to be used as the Key fields for sorting. When sorting, any number of fields may be specified as Key fields—the fields that are used to control the Sort sequence. The order of the Sort is also specified. The Sort may be in Ascending or Descending sequence on the various Key data fields involved.

Using specifies the data file to be used as input into the Sort. Giving specifies the data file that is to be the output of the Sort. When utilizing the Sort statement, the input and output files cannot be Open by the program. The Sort will take care of all I-O against these files, including the Open, Close, Read, Write, and Close statements. Listing 17.1 is a simple program to sort the Dealer.TXT file in the manner described.

The first part of the program is the normal housekeeping and the Select for the input file for the sort.

Listing 17.1 Simple Sort Example

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt17a. 000004* Simple Sort Example 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. 000009 Input-Output Section. 000010 File-Control. 000011 Select Dealer-Text Assign To “Dealer.TXT” 000012 Organization Line Sequential 000013 Access Sequential. The next Select is for the Sort Work File. The name chosen, Sort-Work, is not significant. Any valid filename will work as well. Sort-Work is descriptive, and it is a good programming practice to name your files as descriptively as possible. Note that the physical filename that the Sort Work File is assigned to is not enclosed in quotation marks. It is not a physical name, but rather is a symbolic name used internally by the compiler.

000014 Select Sort-Work Assign To Dealer-Sort-Work. The FD for the input file is the regular, normal FD.

000015 Data Division. 000016 File Section. 000017 Fd Dealer-Text. 000018 01 Dealer-Record. 000019 03 Dealer-Number Pic X(8). 000020 03 Dealer-Name. 000021 05 Last-Name Pic X(25). 000022 05 First-Name Pic X(15). 000023 05 Middle-Name Pic X(10). 000024 03 Address-Line-1 Pic X(50). 000025 03 Address-Line-2 Pic X(50). 000026 03 City Pic X(40). 000027 03 State-Or-Country Pic X(20). 000028 03 Postal-Code Pic X(15). 000029 03 Home-Phone Pic X(20). 000030 03 Work-Phone Pic X(20). 000031 03 Other-Phone Pic X(20). 000032 03 Start-Date Pic 9(8). 000033 03 Last-Rent-Paid-Date Pic 9(8). 000034 03 Next-Rent-Due-Date Pic 9(8). 000035 03 Rent-Amount Pic 9(4)v99. 000036 03 Consignment-Percent Pic 9(3). 000037 03 Last-Sold-Amount Pic S9(7)v99. 000038 03 Last-Sold-Date Pic 9(8). 000039 03 Sold-To-Date Pic S9(7)v99. 000040 03 Commission-To-Date Pic S9(7)v99. 000041 03 Filler Pic X(15). The SD describes the Sort Work File record. For convenience, the same record layout as the input file has been used.

000042 Sd Sort-Work. 000043 01 Sort-Record.

000044 03 Dealer-Number Pic X(8). 000045 03 Dealer-Name. 000046 05 Last-Name Pic X(25). 000047 05 First-Name Pic X(15). 000048 05 Middle-Name Pic X(10). 000049 03 Address-Line-1 Pic X(50). 000050 03 Address-Line-2 Pic X(50). 000051 03 City Pic X(40). 000052 03 State-Or-Country Pic X(20). 000053 03 Postal-Code Pic X(15). 000054 03 Home-Phone Pic X(20). 000055 03 Work-Phone Pic X(20). 000056 03 Other-Phone Pic X(20). 000057 03 Start-Date Pic 9(8). 000058 03 Last-Rent-Paid-Date Pic 9(8). 000059 03 Next-Rent-Due-Date Pic 9(8). 000060 03 Rent-Amount Pic 9(4)v99. 000061 03 Consignment-Percent Pic 9(3). 000062 03 Last-Sold-Amount Pic S9(7)v99. 000063 03 Last-Sold-Date Pic 9(8). 000064 03 Sold-To-Date Pic S9(7)v99. 000065 03 Commission-To-Date Pic S9(7)v99. 000066 03 Filler Pic X(15). 000067 Working-Storage Section. 000068 Procedure Division. 000069 Chapt17a-Start. The Sort statement is very simply stated. The filename specified after Sort is always the Sort Work File and must be described with an SD entry in the File Section. The fields you wish to sort on are specified after either Ascending or Descending Key, depending on whether you want to have the fields sorted from lowest value to highest or from highest to lowest.

The Using clause specifies the input file to be used by the Sort. Giving specifies the file that is to be created by the Sort. The files specified with Using and Giving can be the same.

000070 Sort Sort-Work Ascending Key Last-Name Of Sort-Record 000071 First-Name Of Sort-Record 000072 Middle-Name Of Sort-Record 000073 Using Dealer-Text 000074 Giving Dealer-Text 000075 Display “Sort Complete” 000076 Stop Run 000077 . Notice that the program had no Open, Close, Read or Write statements. The Sort performs all of these operations automatically. Sorting files in COBOL is extremely simple!

Enter, compile, and run this program. Use a text editor, such as Notepad or WordPad to edit the Dealer.TXT file after running the program. Notice the sort sequence of the data file.

Change the program to sort the file in Descending Key sequence instead of Ascending Key sequence. The Sort statement is the only one that needs to change:

000070 Sort Sort-Work Descending Key Last-Name Of Sort-Record 000071 First-Name Of Sort-Record 000072 Middle-Name Of Sort-Record Compile the program with the changes and run it again. Edit the output with WordPad again and notice how the sort sequence changes.

The Sort statement can sort using complex combinations of Ascending and Descending Key fields. For example, you can sort the dealer file Descending by state and the names Ascending under state.

000070 Sort Sort-Work Descending Key State-Or-Country Of Sort-Record 000071 Ascending Key Last-Name Of Sort-Record 000072 First-Name Of Sort-Record 000073 Middle-Name Of Sort-Record The input and output files from a sort need not be the same file type. In Hour 14, an Indexed file was created from the Line Sequential file Dealers.TXT. You used a regular COBOL program to handle the Open, Read, Write, and Close statements. This same task can be accomplished with a Sort. You simply need to specify the Indexed file as the output in the Giving clause of the Sort statement.

Image

When an Indexed file is specified in the Giving clause of a Sort statement, the Sort Key must be the same as the Primary Key of the Indexed file. In addition, the SD must match the FD for record size and the location and length of the Primary Key field.

Listing 17.2 creates the Indexed Dealer.Dat file from the Line Sequential Dealer.TXT file, using a Sort.

Listing 17.2 Create An Indexed File From A Sequential File using Sort

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt17d. 000004* Create An Indexed File From A Sequential File Using Sort 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. 000009 Input-Output Section. 000010 File-Control. The Select statements are provided for all three files: the Line Sequential input file, the Indexed output file, and the Sort Work File.

000011 Select Dealer-Text Assign To “Dealer.TXT” 000012 Organization Line Sequential 000013 Access Sequential. 000014 Select Dealer-File Assign To “Dealer.Dat” 000015 Organization Is Indexed 000016 Record Key Dealer-Number Of Dealer-Record 000017 Alternate Key Dealer-Name Of Dealer-Record 000018 Access Is Sequential. 000019 Select Sort-Work Assign To Dealer-Sort-Work. The File Section of the Data Division contains the FD and SD, File and Sort Definitions. Notice that the record layouts have been simplified to contain only the essential data to complete the desired Sort operation.

000020 Data Division. 000020 Data Division. 000021 File Section. 000022 Fd Dealer-File. 000023 01 Dealer-Record. 000024 03 Dealer-Number Pic X(8). 000025 03 Dealer-Name. 000026 05 Last-Name Pic X(25). 000027 05 First-Name Pic X(15). 000028 05 Middle-Name Pic X(10). 000029 03 Filler Pic X(318). 000030 Fd Dealer-Text. 000031 01 Text-Record Pic X(376). 000032 Sd Sort-Work. 000033 01 Sort-Record. 000034 03 Dealer-Number Pic X(8). 000035 03 Dealer-Name. 000036 05 Last-Name Pic X(25). 000037 05 First-Name Pic X(15). 000038 05 Middle-Name Pic X(10). 000039 03 Filler Pic X(318). 000040 Working-Storage Section. 000041 Procedure Division. 000042 Chapt17d-Start. The Sort statement is coded so that the Sort Key fields and sequence match the Sort Key of the output file. If they do not, the compiler issues a warning and the program does not compile.

000043 Sort Sort-Work Ascending Key Dealer-Number Of Sort-Record 000044 Using Dealer-Text 000045 Giving Dealer-File 000046 Display “Sort Complete” 000047 Stop Run 000048 . If you compile and run this program, it creates a new Dealer.Dat file from the Line Sequential file Dealer.TXT. This method of creating the Indexed file, unlike the version from Hour 14, is not sensitive to the order of the data in the input file. The Sort statement takes care of that problem and creates the Indexed file in the proper sequence.

Manipulating Data During the Sort In addition to the simple sorts discussed in the previous section, COBOL allows you to manipulate the data going into and coming out of the Sort. This feature allows a single program to read a data file, manipulate the data to a great degree, sort it, and produce output based on this data.

This diversity is handled by coding Input and Output procedures on the Sort. These procedures permit you to create a Sort file from various input sources—not just from a single input file. When utilizing the Input and Output procedure, the Sort file does not need to have the same record layout as the input file. The output file does not need to have the same layout as the Sort file. In fact, in some cases the output file is not created at all!

The Input Procedure The Input Procedure allows you to restrict the records that are used in the Sort. When an Input Procedure is specified, you are responsible for the file handling necessary to build the Sort records. However, you do not code any Open, Close, or Write statements for the Sort Work File.

The Input Procedure specified is performed to create the Sort records, which are released to the Sort. The Input Procedure is performed only once for each Sort statement coded. You must handle the necessary processing loop. When the Input Procedure is complete, the Sort Work File is sorted in the sequence specified.

The statement that writes records to the Sort Work File is the Release statement, and its coding is similar to the Write statement. You may Release a Sort record by using the From clause to build the Sort record in Working-Storage if desired. As with the Write statement, the data in the record description area of the Sort record cannot be relied on after a Release statement is executed.

By using the Input Procedure, you can create a Sort Work File and output file such that the record layouts differ, unlike Using and Giving in which the record layouts had to be the same.

Listing 17.3 illustrates the use of an Input Procedure to select dealer records with a state of “CA” from the file. Only the name and address information is selected for the Sort Work File and output file.

Listing 17.3 Sort Example With An Input Procedure

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt17e. 000004* Sort Example With An Input Procedure. 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. 000009 Input-Output Section. Select statements are coded for all three files: the Indexed input file, Dealer.Dat; the output Line Sequential file, Address.TXT; and the Sort Work File.

000010 File-Control. 000011 Select Dealer-File Assign To “Dealer.Dat” 000012 Organization Indexed 000013 Record Key Dealer-Number Of Dealer-Record 000014 Alternate Record Key Dealer-Name Of Dealer-Record 000015 Access Sequential 000016 File Status Dealer-Status. 000017 Select Address-File Assign To “Address.Txt” 000018 Organization Line Sequential 000019 Access Sequential. 000020 Select Sort-Work Assign To Dealer-Sort-Work. 000021 Data Division. 000022 File Section. Notice that the FD for the dealer file does not match the SD for the Sort Work File. The FD for the output file simply has the same number of characters reserved in the record as the Sort Work File. Because the Giving clause is being used to create the file, the individual fields that make up the record need not be defined.

000023 Fd Dealer-File. 000024 01 Dealer-Record. 000025 03 Dealer-Number Pic X(8). 000026 03 Dealer-Name. 000027 05 Last-Name Pic X(25). 000028 05 First-Name Pic X(15). 000029 05 Middle-Name Pic X(10). 000030 03 Address-Line-1 Pic X(50). 000031 03 Address-Line-2 Pic X(50). 000032 03 City Pic X(40). 000033 03 State-Or-Country Pic X(20). 000034 03 Postal-Code Pic X(15). 000035 03 Home-Phone Pic X(20). 000036 03 Work-Phone Pic X(20). 000037 03 Other-Phone Pic X(20). 000038 03 Start-Date Pic 9(8). 000039 03 Last-Rent-Paid-Date Pic 9(8). 000040 03 Next-Rent-Due-Date Pic 9(8). 000041 03 Rent-Amount Pic 9(4)v99. 000042 03 Consignment-Percent Pic 9(3). 000043 03 Last-Sold-Amount Pic S9(7)v99. 000044 03 Last-Sold-Date Pic 9(8). 000045 03 Sold-To-Date Pic S9(7)v99. 000046 03 Commission-To-Date Pic S9(7)v99. 000047 03 Filler Pic X(15). 000048 Sd Sort-Work. 000049 01 Sort-Record. 000050 03 Dealer-Name. 000051 05 Last-Name Pic X(25). 000052 05 First-Name Pic X(15). 000053 05 Middle-Name Pic X(10). 000054 03 Address-Line-1 Pic X(50). 000055 03 Address-Line-2 Pic X(50). 000056 03 City Pic X(40). 000057 03 State-Or-Country Pic X(20). 000058 03 Postal-Code Pic X(15). 000059 Fd Address-File. 000060 01 Address-Record Pic X(225). The File Status field and other necessary fields are coded in Working-Storage.

000061 Working-Storage Section. 000062 01 Done-Flag Pic X Value Spaces. 000063 88 All-Done Value “Y”. 000064 01 Dealer-Status Pic XX Value “00”. 000065 Procedure Division. Declaratives capture any unexpected file errors on the input file. Although none are expected, it is a good practice to code for them. The All-Done flag is Set to true if any errors are encountered. This flag status terminates the Input Procedure loop coded later in the program.

000066 Declaratives. 000067 Dealer-File-Error Section. 000068 Use After Standard Error Procedure On Dealer-File. 000069 Dealer-Error. 000070 Display “Unhandled error on Dealer File” Dealer-Status 000071 Set All-Done To True 000072 . 000073 End Declaratives. 000074 Chapt17e-Start. The Sort statement sorts the Sort Work File based on the last, first, and middle names. Because the file was organized to appear in this sequence with a Group Level item, you could change the Sort Key to Dealer-Name Of Sort-Record and achieve the same result. The statement as coded is a little more explicit, however.

The Input Procedure name is not significant. You may call it anything that you desire. One common mistake is to assume that the Input Procedure will be executed repeatedly until the input file has been read. In fact, the Input Procedure is performed only once.

000075 Sort Sort-Work Ascending Key Last-Name Of Sort-Record 000076 First-Name Of Sort-Record 000077 Middle-Name Of Sort-Record 000078 Input Procedure Sort-In 000079 Giving Address-File 000080 Display “Sort Complete” 000081 Stop Run 000082 . The Input Procedure, Sort-In, handles the Open, Read, and Close statements of the input file. Notice that if the state is not “CA”, the Sort record is not released, which limits the Sort to records where the state is “CA”. Using an Input Procedure to select the desired records can speed processing of large volumes of data.

By using the same field names in the Dealer-Record and Sort-Record, you are able to utilize Move with Corresponding. Notice that more fields are defined in the Dealer-Record than in Sort-Record, yet Move with Corresponding correctly moves only those fields where the field names match.

Note the Close of the input file after processing is complete. No Open or Close statements are coded for the Sort Work File. The only operation relating to the Sort Work File releases the record to the Sort.

000083 Sort-In. 000084 Open Input Dealer-File 000085 Perform Until All-Done 000086 Read Dealer-File 000087 At End Set All-Done To True 000088 Not At End 000089 If State-Or-Country Of Dealer-Record = “CA” 000090 Move Corresponding Dealer-Record To Sort-Record 000091 Release Sort-Record 000092 End-If 000093 End-Read 000094 End-Perform 000095 Close Dealer-File 000096 .

The Output Procedure The Input Procedure processed data before the Sort. If you want to process data after the Sort, you may code an Output Procedure with your Sort statement. The Output Procedure is responsible for all necessary file access. The Output Procedure is frequently used to create a printed report after input data is sorted. The Output Procedure does not necessarily have to create a sorted output file.

Like the Input Procedure, the Output Procedure is performed only once. It is executed immediately after the Sort Work File is sorted into the desired sequence. You are responsible for coding the processing loop necessary for the Output Procedure to work properly.

In the Output Procedure, you may Read records from the Sort Work File. You do not code normal Open, Read, or Close statements. The Sort positions the file properly and handles any necessary internal Open and Close operations. The Return statement retrieves records from the sorted Sort Work File. Return behaves the same as a Sequential Read. You must code an At End clause to detect the end of file. You may Return the record into another data area, just as you can with Read, by using Return with an Into clause.

In Hour 16, “Updating Indexed File Records,” the exercise was to modify the record layout of the dealer file to add four fields: Last-Sold-Amount, Last-Sold-Date, Sold-To-Date and Commission-To-Date. These fields were to be initialized to zeros. Running the program in Listing 17.2 (Chap17d) that creates the Indexed Dealer.Dat file erases that file and eliminates the work you did zeroing those fields. This happens because the input text file does not include the new fields. You may run the program you created again to fix the problem, or you may modify the Sort program to include an Output Procedure that initializes these fields as they are written. The program in Listing 17.4 does just that, and counts the records returned from the Sort. This count is displayed after the Output Procedure is executed.

The program has all of the normal Select and File Section entries.

Listing 17.4 Sort Example With An Output Procedure

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt17f. 000004* Sort Example With Output Procedure 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. 000009 Input-Output Section. 000010 File-Control. 000011 Select Dealer-Text Assign To “Dealer.TXT” 000012 Organization Line Sequential 000013 Access Sequential. 000014 Select Dealer-File Assign To “Dealer.Dat” 000015 Organization Is Indexed 000016 Record Key Dealer-Number Of Dealer-Record 000017 Alternate Key Dealer-Name Of Dealer-Record 000018 Access Is Sequential 000019 File Status Is Dealer-Status. 000020 Select Sort-Work Assign To Dealer-Sort-Work. 000021 Data Division. 000022 File Section. 000023 Fd Dealer-File. 000024 01 Dealer-Record. 000025 03 Dealer-Number Pic X(8). 000026 03 Dealer-Name. 000027 05 Last-Name Pic X(25). 000028 05 First-Name Pic X(15). 000029 05 Middle-Name Pic X(10). 000030 03 Address-Line-1 Pic X(50). 000031 03 Address-Line-2 Pic X(50). 000032 03 City Pic X(40). 000033 03 State-Or-Country Pic X(20). 000034 03 Postal-Code Pic X(15). 000035 03 Home-Phone Pic X(20). 000036 03 Work-Phone Pic X(20). 000037 03 Other-Phone Pic X(20). 000038 03 Start-Date Pic 9(8). 000039 03 Last-Rent-Paid-Date Pic 9(8). 000040 03 Next-Rent-Due-Date Pic 9(8). 000041 03 Rent-Amount Pic 9(4)v99. 000042 03 Consignment-Percent Pic 9(3). 000043 03 Last-Sold-Amount Pic S9(7)v99. 000044 03 Last-Sold-Date Pic 9(8). 000045 03 Sold-To-Date Pic S9(7)v99. 000046 03 Commission-To-Date Pic S9(7)v99. 000047 03 Filler Pic X(15). 000048 Fd Dealer-Text. 000049 01 Text-Record Pic X(376). 000050 Sd Sort-Work. 000051 01 Sort-Record. 000052 03 Dealer-Number Pic X(8). 000053 03 Dealer-Name. 000054 05 Last-Name Pic X(25). 000055 05 First-Name Pic X(15). 000056 05 Middle-Name Pic X(10). 000057 03 Filler Pic X(318). Working-Storage contains the flag used to control the processing loop of the Output Procedure and a field for the record count from the Sort.

000058 Working-Storage Section. 000059 01 Record-Count Pic 9(5) Value Zeros. 000060 01 Dealer-Status Pic XX Value “00”. 000061 01 Done-Flag Pic X Value Spaces. 000062 88 All-Done Value “Y”. Declaratives are coded to handle any errors that might occur when creating the Indexed file. None are likely to occur; however, coding the Declaratives will make you aware of any error conditions that occur.

000063 Procedure Division. 000064 Declaratives. 000065 Dealer-File-Error Section. 000066 Use After Standard Error Procedure On Dealer-File. 000067 Dealer-Error. 000068 Display “Unhandled error on Dealer File” Dealer-Status 000069 Set All-Done To True 000070 . 000071 End Declaratives. This particular Sort does not require an Input Procedure. However, in COBOL you may use an Input Procedure and an Output Procedure in the same Sort statement.

000072 Chapt17f-Start. 000073 Sort Sort-Work Ascending Key Dealer-Number Of Sort-Record 000074 Using Dealer-Text 000075 Output Procedure Sort-Out 000076 Display “Sort Complete with ” Record-Count “ Records.” 000077 Stop Run 000078 . The Output Procedure controls the creation of the Indexed Dealer-File. You are responsible for coding all necessary Open, Write, and Close statements. The Return statement, like Read, uses the name defined in the FD, not the record description, to describe the data being returned. The At End clause handles the end-of-file processing. Regular record processing occurs after the Not At End clause. Notice the use of the End-Return explicit scope terminator.

000079 Sort-Out. 000080 Open Output Dealer-File 000081 Perform Until All-Done 000082 Return Sort-Work Into Dealer-Record 000083 At End Set All-Done To True 000084 Not At End 000085 Add 1 To Record-Count 000086 Move Zeros To Last-Sold-Amount 000087 Last-Sold-Date 000088 Sold-To-Date 000089 Commission-To-Date 000090 Write Dealer-Record 000091 End-Return 000092 End-Perform 000093 Close Dealer-File 000094 . Image

In these Sort examples, the various record sizes and layouts of the input file, output file, and Sort Work File have been the same. However, when using Sort, you may use records of varying sizes. The only restriction is that no input record may be longer than the Sort work record, and if variable-length records are used, none may be shorter than the shortest allowed record in the Sort Work File. If you wanted to sort the dealer file into a text file and shorten the output record to not include some of the extra information at the end of the file, you could modify the FD on the output file to terminate after the last desired field.

When sorting a file, you may encounter duplicate Sort Keys. Duplicates are allowed and will cause no problems. The order of the records with the duplicate Key fields in the Sort file is undetermined. You can control the order, forcing the duplicates to appear in the same order as the input file, by adding the word Duplicates, which is short for Duplicates In Order, to the Sort statement.

000073 Sort Sort-Work Ascending Key Dealer-Number Of Sort-Record 000074 With Duplicates 000075 Using Dealer-Text 000076 Output Procedure Sort-Out

Summary In this hour, you learned the following:

• Files can be sorted quickly and easily by using the Sort statement.

• The input file can be simply specified with a Using clause, and the output file with a Giving clause. No Open, Read, Write, or Close statements need to be coded for a simple sort that utilizes Using and Giving.

• Sort Work Files must have Select statements under File-Control and entries under the File Section. The entry under the File Section, however, is not the normal FD, but instead is an SD.

• You can use an Input Procedure to manipulate and select records for the Sort.

• When using an Input Procedure, you must handle the Open, Read, and Close statements associated with the input file or files.

• Records are written to the Sort Work File in the Input Procedure by coding the Release statement.

• Records can be returned directly from the Sort Work File by using an Output Procedure.

• The Output Procedure does not necessarily have to create a sorted output file. Any logic you desire may be executed in the Output Procedure.

• The Return statement can read sorted records from the Sort Work File.

• You can use any combination of Using, Giving, Input Procedure, and Output Procedure with a Sort statement.

Q&A Q What happens to the Sort Work File when the Sort is finished?

A On most systems, the Sort Work File is automatically deleted after the processing associated with the Sort statement is complete.

Q I need to manipulate some fields for the Sort. Should I modify them in the Input Procedure or in the Output Procedure?

A If any of the fields you are modifying are used as Sort Keys, you should modify them in the Input Procedure. Remember that records are not sorted as they go into the Input Procedure, so modifying the Sort Key will not adversely affect the Sort.

Q I noticed that in the examples, there were no performs outside of the Input and Output Procedures. Am I restricted in what may be performed?

A Only slightly. You may not execute another Sort statement within an Input or Output Procedure. Also you may not execute a Return within an Input Procedure, and you may not execute a Release in an Output Procedure. Otherwise, you are free to code any kind of statements or logic you desire. Just remember that the Input and Output Procedures are performed only once per sort.

Q I’m still a little unclear. Which file is actually sorted? Is it the input file?

A No, it’s not the input file. The Sort Work File is the one that is sorted, which explains why you can manipulate data before the sort, using an Input Procedure. Using a Sort Work File also ensures that records returned from the Sort in the Output Procedure are in sorted sequence.

Workshop To help reinforce your understanding of the material presented in this hour, refer to the section “Quiz and Exercise Questions and Answers” that can be found on the CD. This section contains quiz questions and exercises for you to complete, as well as the corresponding answers.

Fair Use Sources

COBOL: COBOL Fundamentals, COBOL Inventor - COBOL Language Designer: 1959 by Howard Bromberg, Norman Discount, Vernon Reeves, Jean E. Sammet, William Selden, Gertrude Tierney, with indirect influence from Grace Hopper, CODASYL, ANSI COBOL, ISO/IEC COBOL; Modern COBOL - Legacy COBOL, IBM COBOL, COBOL keywords, COBOL data structures - COBOL algorithms, COBOL syntax, Visual COBOL, COBOL on Windows, COBOL on Linux, COBOL on UNIX, COBOL on macOS, Mainframe COBOL, IBM i COBOL, IBM Mainframe DevOps, COBOL Standards, COBOL Paradigms (Imperative COBOL, Procedural COBOL, Object-Oriented COBOL - COBOL OOP, Functional COBOL), COBOL syntax, COBOL installation, COBOL containerization, COBOL configuration, COBOL compilers, COBOL IDEs, COBOL development tools, COBOL DevOps - COBOL SRE, COBOL data science - COBOL DataOps, COBOL machine learning, COBOL deep learning, COBOL concurrency, COBOL history, COBOL bibliography, COBOL glossary, COBOL topics, COBOL courses, COBOL Standard Library, COBOL libraries, COBOL frameworks, COBOL research, Grace Hopper, COBOL GitHub, Written in COBOL, COBOL popularity, COBOL Awesome list, COBOL Versions. (navbar_cobol)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


sams_teach_yourself_cobol_in_24_hours_-_hour_17_sorting.txt · Last modified: 2024/04/28 03:37 (external edit)