sams_teach_yourself_cobol_in_24_hours_-_hour_15_reading_indexed_file_records

Sams Teach Yourself COBOL in 24 Hours - Hour 15 Reading Indexed File Records

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 15 Reading Indexed File Records

In the previous hour, you learned how to create an Indexed file. In this hour, you learn different ways to Read the records in the file. You will learn about

• The different access methods for Indexed files, such as Sequential, Random, and Dynamic.

• How to position the file using the Start statement.

• How to Read records randomly.

• How to Read records sequentially from an Indexed file.

Indexed files can provide superior performance and response time in your programs. Imagine how long the user would have to wait to recall the information for a dealer, when the dealer number is entered, if the program had to Read through all the records in a large file. Indexed files provide virtually instant access to the information if the Key field is known. Even if one of the Key fields in not known, Indexed files can still narrow the search and speed the location of the information.

Various Access Methods As mentioned briefly in Hour 14, “Indexed Files,” COBOL offers several ways to access an Indexed file, depending on the situation. Although these methods have much in common, to a large extent, the efficiency of Indexed file access varies with each COBOL compiler and the environment upon which it runs. In addition, these different access methods have specific performance advantages and disadvantages.

Sequential access, for example, allows the file to be processed from front to back, from lowest Primary Key to highest, using the same programming statements as a normal Sequential file. Sequential access can be put to good use when the entire file is to be processed. On the other hand, Random access provides instant access to a specific record and can be a very fast way to retrieve information from an Indexed file. Dynamic access allows both Sequential and Random access. Dynamic access offers the best of both worlds but has the disadvantage of being slightly more cumbersome than either Random or Sequential access.

Sequential Access Image

Think of an Indexed file as a book. Each record in the file is a page in the book. The Key for the records is the page number. Sequential access allows you to Read through the book, starting at the first page and ending after the last. You cannot jump ahead in the book. You cannot jump backward in the book. You can only go forward, page by page. You must Read every page, and you cannot skip any pages. If you close the book and Open it again, you must start over from the front of the book.

Sequential access of an Indexed file works exactly as described in this book analogy. Sequential access is specified in the Select statement for the file:

000058 Select Dealer-File Assign To “Dealer.Dat” 000059 Organization Indexed 000060 Access Sequential 000061 Record Key Dealer-Number Of Dealer-Record 000062 Alternate Record Key Dealer-Name Of Dealer-Record 000063 File Status Dealer-Status. In order to Read data from an Indexed file, it must be opened for Input. The Open statement is very simple:

000101 Open Input Dealer-File When applied against an Indexed file, with Access Sequential, this Open statement allows you to Read data records from the Indexed file. The Read statement operates exactly the same way with an Indexed file Open with Sequential access as it does with a regular Sequential file. Each subsequent Read statement returns the next record in the file, in Primary Key sequence. The At End condition is true if a Read is attempted after the last record of a file is read. The File Status returned is 10 if the end of file is reached.

The program in Listing 15.1 returns a record in the file every time the user presses Enter until the answer to the question Read another record? is N or the end of file is reached.

Image

Before running this program, you might want to re-run the program in Listing 14.1 to create the file from the provided DEALER.TXT file.

Listing 15.1 Sequentially Read An Indexed File

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt15a. 000004* Sequentially Read An Indexed File 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-File Assign To “Dealer.Dat” 000012 Organization Indexed 000013 Access Sequential 000014 Record Key Dealer-Number 000015 Alternate Record Key Dealer-Name 000016 File Status Dealer-Status. 000017 Data Division. 000018 File Section. 000019 Fd Dealer-File. 000020 01 Dealer-Record. 000021 03 Dealer-Number Pic X(8). 000022 03 Dealer-Name. 000023 05 Last-Name Pic X(25). 000024 05 First-Name Pic X(15). 000025 05 Middle-Name Pic X(10). 000026 03 Address-Line-1 Pic X(50). 000027 03 Address-Line-2 Pic X(50). 000028 03 City Pic X(40). 000029 03 State-Or-Country Pic X(20). 000030 03 Postal-Code Pic X(15). 000031 03 Home-Phone Pic X(20). 000032 03 Work-Phone Pic X(20). 000033 03 Other-Phone Pic X(20). 000034 03 Start-Date Pic 9(8). 000035 03 Last-Rent-Paid-Date Pic 9(8). 000036 03 Next-Rent-Due-Date Pic 9(8). 000037 03 Rent-Amount Pic 9(4)v99. 000038 03 Consignment-Percent Pic 9(3). 000039 03 Filler Pic X(50). 000040 Working-Storage Section. 000041 01 Dealer-Status Pic X(2) Value Spaces. 000042 88 Dealer-Success Value “00”. 000043 01 Show-Next-Record Pic X Value “Y”. 000044 01 Process-Flag Pic X Value Spaces. 000045 88 End-Process Value “Y”. 000046 Screen Section. 000047 01 Data-Entry-Screen 000048 Blank Screen, Auto 000049 Foreground-Color Is 7, 000050 Background-Color Is 1. 000051* 000052 03 Screen-Literal-Group. 000053 05 Line 01 Column 30 Value “Darlene’s Treasures” 000054 Highlight Foreground-Color 4 Background-Color 1. 000055 05 Line 03 Column 30 Value “Tenant Display Program” 000056 Highlight. 000057 05 Line 4 Column 01 Value “Number: ”. 000058 05 Line 5 Column 01 Value “Name, Last: ”. 000059 05 Line 5 Column 39 Value “First: ”. 000060 05 Line 5 Column 62 Value “Middle: ”. 000061 05 Line 6 Column 01 Value “Address 1: ”. 000062 05 Line 7 Column 01 Value “Address 2: ”. 000063 05 Line 8 Column 01 Value “City: ”. 000064 05 Line 9 Column 01 Value “Country/State: ”. 000065 05 Line 9 Column 36 Value “Postal Code: ”. 000066 05 Line 11 Column 01 Value “Phone/Home: ”. 000067 05 Line 11 Column 34 Value “Work: ”. 000068 05 Line 12 Column 06 Value “Other: ”. 000069 05 Line 14 Column 01 Value “Start Date: ”. 000070 05 Line 14 Column 24 Value “Last Paid Date: ”. 000071 05 Line 14 Column 51 Value “Next Rent Due on: ”. 000072 05 Line 15 Column 01 Value “Rent Amount: ”. 000073 05 Line 16 Column 01 Value “Consignment Percent: ”. 000074 05 Line 22 Column 01 000075 Value “Display next Record? (Y/N):”. 000076* 000077 03 Required-Reverse-Group Reverse-Video. 000078 05 Line 4 Column 13 Pic X(8) From Dealer-Number. 000079 05 Line 5 Column 13 Pic X(25) From Last-Name. 000080 05 Line 5 Column 46 Pic X(15) From First-Name. 000081 05 Line 5 Column 70 Pic X(10) From Middle-Name. 000082 05 Line 6 Column 15 Pic X(50) From Address-Line-1. 000083 05 Line 7 Column 15 Pic X(50) From Address-Line-2. 000084 05 Line 8 Column 15 Pic X(40) From City. 000085 05 Line 9 Column 15 Pic X(20) From State-Or-Country. 000086 05 Line 9 Column 50 Pic X(15) From Postal-Code. 000087 05 Line 11 Column 13 Pic X(20) From Home-Phone. 000088 05 Line 11 Column 41 Pic X(20) From Work-Phone. 000089 05 Line 12 Column 13 Pic X(20) From Other-Phone. 000090 05 Line 14 Column 13 Pic 99/99/9999 From Start-Date. 000091 05 Line 14 Column 40 Pic 99/99/9999 000092 From Last-Rent-Paid-Date. 000093 05 Line 14 Column 69 Pic 99/99/9999 000094 From Next-Rent-Due-Date. 000095 05 Line 15 Column 14 Pic Z,ZZZ.99 From Rent-Amount. 000096 05 Line 16 Column 22 Pic ZZ9 From Consignment-Percent. 000097 05 Line 22 Column 29 Pic X Using Show-Next-Record. 000098* 000099 000100 Procedure Division. 000101 Chapt15a-Start. 000102 Perform Open-File 000103 If Dealer-Success 000104 Perform Process-Screen Until Show-Next-Record = “N” Or 000105 Show-Next-Record = “n” Or 000106 End-Process 000107 Perform Close-File 000108 End-If 000109 Stop Run 000110 . 000111 Process-Screen. 000112 Read Dealer-File 000113 At End Set End-Process To True 000114 Not At End 000115 Perform Display-And-Accept 000116 End-Read 000117 . 000118 Display-And-Accept. 000119 Display Data-Entry-Screen 000120 Accept Data-Entry-Screen 000121 . 000122 Open-File. 000123 Open Input Dealer-File 000124 . 000125 Close-File. 000126 Close Dealer-File 000127 . When you run this program, notice that the records are read in Primary Key sequence. The names do not appear in alphabetical order; instead you see the lowest account number and progress to the higher ones.

The Start Statement With a book, you can open to any page and begin reading. The same is true of an Indexed file accessed in Sequential mode. You can Start at any position within the file and then Read records. You cannot go backward, and if you Close the file, you have to Start over. But you can begin reading anywhere in the file by using the Start statement.

The Start statement allows you to specify the position in the file where the next Read will occur. With the Start statement, you specify the file you want to position—and the location—in reference to the Key field. Before issuing the Start, you place a value in the Key field to control the positioning. You can Start the file on a record equal to the Key field, greater than the Key field, greater than or equal to the Key field, or not less than the Key field. You may not specify less than in the Start statement. For example, if you want to begin processing on the account numbers beginning with the letter C, you can code the following statements after the Open and before any Read statements:

000412 Move “C” to Dealer-Number 000413 Start Dealer-File Key Not < Dealer-Number Image

Most compilers allow you to code the Start statement without using the Invalid Key clause and without having Declaratives associated with the file. The COBOL standard requires the presence of either Declaratives or an Invalid Key clause. To ensure that your program can compile on standard COBOL compilers, you should either use Declaratives or specify the Invalid Key clause.

One new File Status value (status 23) can be returned when you use the Start statement to position an Indexed file for a Sequential Read. Status 23 means record not found. This status is returned after a Start statement if a record cannot be found that matches the requested position in the file. This File Status is returned if there are no greater keys in the file or the specific Key was not found when Start with Key = is used.

To Start processing the file with account numbers that begin with C, you can use the following code in the Procedure Division:

000098 Chapt15b-Start. 000099 Perform Open-File 000100 If Dealer-Success 000101 Move “C” To Dealer-Number 000102 Start Dealer-File Key Not < Dealer-Number 000103 Invalid Key Set End-Process To True 000104 End-Start 000105 Perform Process-Screen Until Show-Next-Record = “N” Or 000106 Show-Next-Record = “n” Or 000107 End-Process 000108 Perform Close-File 000109 End-If 000110 Stop Run 000111 . An interesting aspect of the Start statement is that it allows you to begin sequentially reading the Indexed file based on the Alternate Key field. This approach is like creating a new page-numbering scheme for your book and then rearranging the pages in the book in the new sequence. COBOL allows you to perform the same task using Indexed files and the Start statement. Instead of specifying the Primary Key on the Start statement, you may specify an Alternate Key field. The following example shows the Procedure Division code necessary to Start reading the file on the Alternate Key, beginning with last names that begin with H.

000098 Chapt15c-Start. 000099 Perform Open-File 000100 If Dealer-Success 000101 Move “H” To Dealer-Name 000102 Start Dealer-File Key Not < Dealer-Name 000103 Invalid Key Set End-Process To True 000104 End-Start 000105 Perform Process-Screen Until Show-Next-Record = “N” Or 000106 Show-Next-Record = “n” Or 000107 End-Process 000108 Perform Close-File 000109 End-If 000110 Stop Run 000111 . When you run the program with this code inserted, the first name that is displayed begins with an H. Each subsequent Read returns the records in name sequence, not in account number sequence as you saw before. The Start statement specifies where to Start in the file and in which Key sequence to Read the file.

What if you want to Start at the beginning of the file, using the Alternate Key? You don’t know what the lowest Key value in the file is, and you want to code a Start statement that ensures access to every record in the file. In this case, use the Start statement but place Low-Values in the Key field. When starting, use the not < phrase, not the > phrase. Using not < ensures that the next record read is the one with Low-Values or something greater in it.

The Start statement might be coded as follows:

000099 Move Low-Values To Dealer-Name 000100 Start Dealer-File Key Not < Dealer-Name 000101 Invalid Key Set End-Process To true If you know the specific record Key you want to Start on, you may use Start with Key = the Key field after it has been filled with the appropriate starting Key, for example:

000099 Move “Jennings” To Last-Name 000100 Move “Shelly” To First-Name 000101 Move “Martin” To Middle-Name 000102 Start Dealer-File Key = Dealer-Name 000103 Invalid Key Set End-Process To True If the name does not exist, you get a File Status 23 and the Invalid Key condition is true. If the name does exist, the very next Read contains the record with this Key value.

Image

The Start statement does not return the record; it only positions the file for the next Read. A Read statement must be executed to retrieve a data record, even after a successful Start.

Random Access Image

You have learned how to access records sequentially from an Indexed file. Another method that can be used to Read records from the file is Random access. Random access is similar to deciding which page of a book to open to, opening exactly on that page, reading it, and then closing the book. You may not read the next page or the preceding page without first closing and reopening the book, but only the page you specified.

The Select statement necessary for Random access is

000058 Select Dealer-File Assign To “Dealer.Dat” 000059 Organization Indexed 000060 Access Random 000061 Record Key Dealer-Number Of Dealer-Record 000062 Alternate Record Key Dealer-Name Of Dealer-Record 000063 File Status Dealer-Status. When reading from the file using Random access, you must place a value in the Key field of the file being read. A simple Move statement is all you need.

000101 Move “L3460” To Dealer-Number The next Read statement for the file returns the record identified by dealer number L3460.

000102 Read Dealer-File If the file contains no record matching the Key specified, a File Status of 23 is returned, which is an Invalid Key condition. You may code the Invalid Key clause after the Read to handle these conditions if you desire.

000102 Read Dealer-File 000103 Invalid Key Display “No Dealer Record Found” 000104 Not Invalid Key Perform Process-Record 000105 End-Read Image

If a Read is not successful, the content of your data record area is not protected. If data was in the area from a previous Read or a value was in the Key field, it may not be there after an unsuccessful Read attempt.

I suggest the use of the End-Read explicit scope terminator whenever you use the Invalid Key clause.

Unless otherwise specified, the Read statement assumes that you are reading via the Primary Key field. It is entirely permissible to Read records from an Indexed file opened in Random mode by the Alternate Key field. When you Read via the Alternate Key, the specific Key field desired must be coded in the Read statement. You must remember to Move the data value required to identify the desired record into the Alternate Key field before the Read.

000100 Move “Alan” To First-Name 000101 Move “Aaron” To Middle-Name 000102 Move “Holmes” To Last-Name 000103 Read Dealer-File Key Dealer-Name 000104 Invalid Key Display “Record Not Found” 000105 Not Invalid Key Perform Process-Record 000106 End-Read If a matching record is not found, a File Status of 23 is returned in the File Status field if one is defined. Declaratives may be specified for the file and will be executed in case of a Read that is not successful. The Key clause specifies the Key field to be used for the Random Read.

Some files may be defined with an Alternate Key that allows duplicates. If a Random Read is attempted against such a Key and a record is identified that has an identical Alternate Key to another record in the file, a File Status of 02 is returned. The record returned is the oldest in the file, the first record added with this Alternate Key value.

Image

The At End clause is not valid for use on a Read statement when the file is Open for Random access. You will never reach the end of the file, the record being read will exist, or it will not.

Random reads are useful when you know the Key information of the record you are trying to retrieve. Random reads are an extremely fast way to retrieve the data record and can be handy in programs that are used to retrieve information about a specific item or individual.

Dynamic Access Image

Dynamic access is the slowest and most versatile of the access methods used to retrieve records from an Indexed file. This type of access is the slowest because of the overhead required for the program to keep track of its position in the file. Dynamic access allows you to retrieve records both randomly and sequentially.

The Select statement used to specify Dynamic access is coded as follows:

000058 Select Dealer-File Assign to “Dealer.Dat” 000059 Organization Indexed 000060 Access Dynamic 000061 Record Key Dealer-Number of Dealer-Record 000062 Alternate Record Key Dealer-Name of Dealer-Record 000063 File Status Dealer-Status. When Dynamic access is specified, you can perform Random reads using exactly the same method as if the file were Open with Random access.

To Read records from the file sequentially from an Indexed file Open with Dynamic access, you must first position the file at a valid record. Correct positioning can be accomplished in three ways.

One method is to issue a Random Read using the desired Key field. If the Read is successful, then you may continue to Read subsequent records from the file sequentially in the order of the Key that was used for the Random Read. Obviously, you need some way to differentiate between a Random Read and a Sequential Read designed to retrieve the next record in the file.

To indicate that the next record in sequence should be returned, you issue a Read statement with the Next clause.

000101 Read Dealer-File Next Record Image

The word record is optional and is specified only to make the code more readable.

The At End clause may be coded to detect the end of file. The Invalid Key clause is not valid on a Read statement with the Next clause. Some additional File Status values may be returned after a Read Next statement is executed. In addition to the File Status values returned as a result of a Read statement, Table 15.1 explains two new File Status values that may be returned after the Read Next statement.

Table 15.1 Additional File Status Values Returned From A Read With Next

Image

The second method that can be used to position the file for a Sequential Read is to issue a Start statement. The Start statement works as previously discussed and sets the position in the file for the next Read Next statement.

Image

Use caution when reading randomly from an Indexed file that is Open with Dynamic access. Any previous Start statements do not retain the associated Key or file positioning. Any subsequent Read Next statements may not return the record you expect in the Key sequence you expect. A successful Read repositions the file and possibly changes the Key by which the file is being read.

The third method to position the file at a valid record is simply to Open the file. When you Open an Indexed file with Dynamic access, the next record pointer is set to the beginning of the file. Subsequent Read statements with the Next clause return records in Primary Key sequence.

The program in Listing 15.2 uses both Sequential and Random access. This program allows the user to input the information for the Key fields and then choose how to retrieve the record by using the function keys. The program uses the Start statement to determine which Key to use for reading sequentially. A function key is provided to clear the screen input.

This first part of the program identifies it and specifies the Special Names necessary to capture and set the cursor position and to capture the various function keys you want to detect.

Listing 15.2 Dynamic Access Example

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt15d. 000004* Dynamic Access Example 000005 Environment Division. 000006 Configuration Section. 000007 Special-Names. 000008 Crt Status Is Keyboard-Status 000009 Cursor Is Cursor-Position. 000010 Source-Computer. IBM-PC. 000011 Object-Computer. IBM-PC. The file is described here. Notice the access mode is Dynamic. This mode allows you to retrieve records either sequentially or randomly from the file.

000012 Input-Output Section. 000013 File-Control. 000014 Select Dealer-File Assign To “Dealer.Dat” 000015 Organization Indexed 000016 Access Dynamic 000017 Record Key Dealer-Number 000018 Alternate Record Key Dealer-Name 000019 File Status Dealer-Status. 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 Address-Line-1 Pic X(50). 000030 03 Address-Line-2 Pic X(50). 000031 03 City Pic X(40). 000032 03 State-Or-Country Pic X(20). 000033 03 Postal-Code Pic X(15). 000034 03 Home-Phone Pic X(20). 000035 03 Work-Phone Pic X(20). 000036 03 Other-Phone Pic X(20). 000037 03 Start-Date Pic 9(8). 000038 03 Last-Rent-Paid-Date Pic 9(8). 000039 03 Next-Rent-Due-Date Pic 9(8). 000040 03 Rent-Amount Pic 9(4)v99. 000041 03 Consignment-Percent Pic 9(3). 000042 03 Filler Pic X(50). The various working fields are described here in Working-Storage. Notice the File Status value clause is coded with an 88 level condition. The condition specified for Dealer-Success to be true is the range of File Status values from 00 through 09. Any File Status value that begins with 0 indicates a successful operation.

000043 Working-Storage Section. 000044 01 Dealer-Status Pic X(2) Value Spaces. 000045 88 Dealer-Success Value “00” Thru “09”. The Keyboard-Status and Cursor-Position fields allow you to capture the key that was pressed and to position the cursor on the first field when the screen is cleared.

000046 01 Keyboard-Status. 000047 03 Accept-Status Pic 9. 000048 03 Function-Key Pic X. 000049 88 F1-Pressed Value X“01”. 000050 88 F2-Pressed Value X“02”. 000051 88 F3-Pressed Value X“03”. 000052 88 F4-Pressed Value X“04”. 000053 88 F5-Pressed Value X“05”. 000054 88 F6-Pressed Value X“06”. 000055 03 System-Use Pic X. 000056 01 Cursor-Position. 000057 03 Cursor-Row Pic 9(2) Value 1. 000058 03 Cursor-Column Pic 9(2) Value 1. The screen description uses the Error-Message to hold any error messages you might need to issue.

000059 01 Error-Message Pic X(50) Value Spaces. The Screen Section describes the input screen. Notice that the only fields that can be used as input are the Number and Name fields. Text has been added to describe the functions of the various keys that are captured.

000060 Screen Section. 000061 01 Data-Entry-Screen 000062 Blank Screen, Auto 000063 Foreground-Color is 7, 000064 Background-Color is 1. 000065 03 Screen-Literal-Group. 000066 05 Line 01 Column 30 Value “Darlene’s Treasures” 000067 Highlight Foreground-Color 4 Background-Color 1. 000068 05 Line 03 Column 30 Value “Tenant Entry Program” 000069 Highlight. 000070 05 Line 4 Column 01 Value “Number: ”. 000071 05 Line 5 Column 01 Value “Name, Last: ”. 000072 05 Line 5 Column 39 Value “First: ”. 000073 05 Line 5 Column 62 Value “Middle: ”. 000074 05 Line 6 Column 01 Value “Address 1: ”. 000075 05 Line 7 Column 01 Value “Address 2: ”. 000076 05 Line 8 Column 01 Value “City: ”. 000077 05 Line 9 Column 01 Value “Country/State: ”. 000078 05 Line 9 Column 36 Value “Postal Code: ”. 000079 05 Line 11 Column 01 Value “Phone/Home: ”. 000080 05 Line 11 Column 34 Value “Work: ”. 000081 05 Line 12 Column 06 Value “Other: ”. 000082 05 Line 14 Column 01 Value “Start Date: ”. 000083 05 Line 14 Column 24 Value “Last Paid Date: ”. 000084 05 Line 14 Column 51 Value “Next Rent Due on: ”. 000085 05 Line 15 Column 01 Value “Rent Amount: ”. 000086 05 Line 16 Column 01 Value “Consignment Percent: ”. 000087 05 Line 22 Column 01 Value “F1-Read Random Number”. 000088 05 Line 22 Column 23 Value “F2-Read Random Name”. 000089 05 Line 22 Column 56 Value “F3-Read Next Number”. 000090 05 Line 23 Column 01 Value “F4-Read Next Name”. 000091 05 Line 23 Column 23 Value “F5-Clear”. 000092 05 Line 23 Column 56 Value “F6-Exit”. 000093 03 Required-Reverse-Group Reverse-Video. 000094 05 Line 4 Column 13 Pic X(8) Using Dealer-Number. 000095 05 Line 5 Column 13 Pic X(25) Using Last-Name. 000096 05 Line 5 Column 46 Pic X(15) Using First-Name. 000097 05 Line 5 Column 70 Pic X(10) Using Middle-Name. 000098 05 Line 6 Column 15 Pic X(50) From Address-Line-1. 000099 05 Line 7 Column 15 Pic X(50) From Address-Line-2. 000100 05 Line 8 Column 15 Pic X(40) From City. 000101 05 Line 9 Column 15 Pic X(20) From State-Or-Country. 000102 05 Line 9 Column 50 Pic X(15) From Postal-Code. 000103 05 Line 11 Column 13 Pic X(20) From Home-Phone. 000104 05 Line 11 Column 41 Pic X(20) From Work-Phone. 000105 05 Line 12 Column 13 Pic X(20) From Other-Phone. 000106 05 Line 14 Column 13 Pic 99/99/9999 From Start-Date. 000107 05 Line 14 Column 40 Pic 99/99/9999 From Last-Rent-Paid-Date. 000108 05 Line 14 Column 69 Pic 99/99/9999 From Next-Rent-Due-Date. 000109 05 Line 15 Column 14 Pic Z,ZZZ.99 From Rent-Amount. 000110 05 Line 16 Column 22 Pic ZZ9 From Consignment-Percent. 000111 05 Line 20 Column 01 Pic X(50) Using Error-Message. The first portion of the actual program opens the dealer file. If the Open fails, an error message is displayed and processing stops. If the Open is successful, then the file is processed until the user presses the F6 key to exit.

000112 Procedure Division. 000113 Chapt15d-Start. 000114 Perform Open-File 000115 If Not Dealer-Success 000116 String “Error Opening Dealer File” 000117 Dealer-Status 000118 Delimited By Size 000119 Into Error-Message 000120 End-String 000121 Perform Display-And-Accept 000122 Else 000123 Initialize Dealer-Record 000124 Perform Process-File Until F6-Pressed 000125 Perform Close-File 000126 End-If 000127 Stop Run 000128 . The Process-File Paragraph displays the screen and accepts the user input. The Paragraph tests, via an Evaluate statement, for the different keystrokes that can be pressed and performs the appropriate function.

000129 Process-File. 000130 Perform Display-And-Accept 000131 Evaluate True 000132 When F1-Pressed 000133 Perform Read-Random-Number 000134 When F2-Pressed 000135 Perform Read-Random-Name 000136 When F3-Pressed 000137 Perform Read-Next-Number 000138 When F4-Pressed 000139 Perform Read-Next-Name 000140 When F5-Pressed 000141 Perform Clear-Screen 000142 When F6-Pressed 000143 Continue 000144 When Other 000145 Continue 000146 End-Evaluate 000147 . The Read-Random-Number Paragraph reads the dealer file randomly via the Primary Key of the file. Notice that no specific Key is specified. If none is specified, then the Primary Key is used. If an error occurs, the exact error is reported to the user via the error message. The only error that you should see reported, baring hardware problems, is 23 for record not found. There is no Move of data to the Key field because this step is automatically handled by the Screen Section.

000148 Read-Random-Number. 000149 Read Dealer-File 000150 Invalid Key 000151 String “Error on Random Read Number” 000152 Dealer-Status 000153 Delimited By Size 000154 Into Error-Message 000155 End-Read 000156 . The Read-Random-Name Paragraph is nearly identical to the Read-Random-Number Paragraph. The only real difference is that the Alternate Key field is specified, so the Read is attempted using that Key.

000157 Read-Random-Name. 000158 Read Dealer-File Key Dealer-Name 000159 Invalid Key 000160 String “Error on Random Read Name” 000161 Dealer-Status 000162 Delimited By Size 000163 Into Error-Message 000164 End-Read 000165 . The Read-Next-Number Paragraph does two things. First is does a Start on the file to position it on the next dealer number. The Key specified is the dealer number. If the Start fails, the reason for the failure is reported to the user in the error message.

If the Start statement is successful, a Read with the next clause is attempted. If the end of the file is reached, it is reported to the user.

000166 Read-Next-Number. 000167 Start Dealer-File Key > Dealer-Number 000168 Invalid Key 000169 String “Start Error Number” 000170 Dealer-Status 000171 Delimited By Size 000172 Into Error-Message 000173 End-Start 000174 If Dealer-Success 000175 Read Dealer-File Next 000176 At End 000177 Move “End of File, Read by Number” To Error-Message 000178 End-Read 000179 End-If 000180 . The Read-Next-Name Paragraph performs a similar Start and Read. The Start statement uses the Dealer-Name field instead of the Dealer-Number. The only difference in the Read with Next statements in the two paragraphs is the text of the error reported. The Read with Next need not specify the Key being used; it is assumed from the last successful Read or Start operation.

000181 Read-Next-Name. 000182 Start Dealer-File Key > Dealer-Name 000183 Invalid Key 000184 String “Start Error Name” 000185 Dealer-Status 000186 Delimited By Size 000187 Into Error-Message 000188 End-Start 000189 If Dealer-Success 000190 Read Dealer-File Next 000191 At End 000192 Move “End of File, Read by Name” To Error-Message 000193 End-Read 000194 End-If 000195 . The Clear-Screen Paragraph clears the dealer record that is used by the screen description and sets the cursor positioning so that the cursor appears in the first field on the screen.

000196 Clear-Screen. 000197 Initialize Dealer-Record 000198 Move 01 To Cursor-Row Cursor-Column 000199 . The Display-And-Accept Paragraph displays the screen and accepts the user input. This Paragraph also clears any remaining error messages after accepting the user input.

000200 Display-And-Accept. 000201 Display Data-Entry-Screen 000202 Accept Data-Entry-Screen 000203 Move Spaces To Error-Message 000204 . The Open and Close statements grant Input access to the file and release the file to the operating system when the program is finished with the file.

000205 Open-File. 000206 Open Input Dealer-File 000207 . 000208 Close-File. 000209 Close Dealer-File 000210 . There are some more advanced ways to use the Start statement. For example, imagine that you are scrolling through the file by name. However, you want to jump to the next record with a last name that begins with the next letter in the alphabet. The Start statement offers a simple way to accomplish that task.

As you know, with Start you must specify the Key field you want to Start on. You may, however, use just a portion of that field and not specify the entire field. For example, change the Record Description of the Dealer-Record to look like this:

000022 FD Dealer-File. 000023 01 Dealer-Record. 000024 03 Dealer-Number Pic X(8). 000025 03 Dealer-Name. 000026 05 Last-Name. 000027 10 Last-Name-First-Letter Pic X. 000028 10 Last-Name-Remainder Pic X(24). 000029 05 First-Name Pic X(15). 000030 05 Middle-Name Pic X(10). 000031 03 Address-Line-1 Pic X(50). 000032 03 Address-Line-2 Pic X(50). 000033 03 City Pic X(40). 000034 03 State-or-Country Pic X(20). 000035 03 Postal-Code Pic X(15). 000036 03 Home-Phone Pic X(20). 000037 03 Work-Phone Pic X(20). 000038 03 Other-Phone Pic X(20). 000039 03 Start-Date Pic 9(8). 000040 03 Last-Rent-Paid-Date Pic 9(8). 000041 03 Next-Rent-Due-Date Pic 9(8). 000042 03 Rent-Amount Pic 9(4)V99. 000043 03 Consignment-Percent Pic 9(3). 000044 03 Filler Pic X(50). Now, when you want to position the file for reading the next record that has a last name that begins with the next letter of the alphabet, you can issue the following Start statement:

000101 Start Dealer-File Key > Last-Name-First-Letter If the current last name is “Berg”, the next record returned after a Read with Next will have a last name of “Colvin”.

The only rule you need to remember is that the field you use must Start is in the same position in the record as the actual Key field.

Summary In this hour, you learned the following:

• Indexed files can be read randomly or sequentially. Random reads are made directly with a known Key, and Sequential reads are made serially, one after the other, based on the Key sequence of the file.

• You can specify the starting position for Sequential reads by using the Start statement.

• The Start statement is valid for both Sequential access and Dynamic access.

• When a Random Read is issued and a matching record is not found, a File Status 23 is returned. Another way to detect this condition is to use the Invalid Key clause of the Read statement because any File Status that begins with 2 indicates an Invalid Key condition.

• Dynamic access allows you to access an Indexed file randomly or sequentially. The statement to Read a record sequentially is the Read statement with the Next clause.

• A Start statement does not return the data record contents; it only positions the file for the next Read.

Q&A Q If I am reading an Indexed file with Access Sequential, how do I ensure that I am reading from the beginning of the file?

A Simply opening the file positions you at the front of the file. If you want to be certain, you can Move Low-Values to the Key field and issue a Start statement with Key Not < Your-Key. The very next Read Starts at the front of the file.

Q I understand that Sequential access allows me to Read the records in an Indexed file sequentially. Because Dynamic access is slower, why would I ever need to use Dynamic access?

A Although both Dynamic access and Sequential access allow you to Read sequentially through an Indexed file, in some cases you may need to execute a Random Read against the same file that you are reading sequentially. Dynamic access allows you to do both. I suggest that you evaluate the requirements of your program carefully and choose the most appropriate access method.

Q How can I ensure that Read with Next reads the file in Key sequence by the Alternate Key field?

A Issue a Start statement that uses the Alternate Key or do a Random Read using the Alternate Key.

Q If both Start and a Random Read position the file so that a Read with Next performs Sequential reads, why do I need to use Start?

A The Read statement with the Next clause can work only if the file is positioned on a record. This condition happens in one of three ways: opening the file, a successful Random Read, or a successful Start. If you do not know the Key value of a record that exists in the file, you cannot perform a successful Random Read. If your Read or Start fails, the Read statement with the Next clause that follows will also fail.

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_15_reading_indexed_file_records.txt · Last modified: 2024/04/28 03:37 (external edit)