[an error occurred while processing this directive]
HP OpenVMS Systems Documentation |
HP COBOL
|
Previous | Contents | Index |
Your program can read an indexed file sequentially, randomly, or dynamically.
Reading an Indexed File Sequentially
Reading indexed records sequentially involves the following:
The READ statement makes the next logical record of an open file available to the program. It skips deleted records and sequentially reads and retrieves only valid records. When the at end condition occurs, execution of the READ statement is unsuccessful (see Chapter 7).
Example 6-32 reads an entire indexed file sequentially beginning with the first record in the file, displaying every record on the terminal.
Example 6-32 Reading an Indexed File Sequentially |
---|
IDENTIFICATION DIVISION. PROGRAM-ID. INDEX03. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FLAVORS ASSIGN TO "DAIRY" ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS ICE-CREAM-MASTER-KEY ALTERNATE RECORD KEY IS ICE-CREAM-STORE-STATE WITH DUPLICATES ALTERNATE RECORD KEY IS ICE-CREAM-STORE-CODE. DATA DIVISION. FILE SECTION. FD FLAVORS. 01 ICE-CREAM-MASTER. 02 ICE-CREAM-MASTER-KEY PIC XXXX. 02 ICE-CREAM-MASTER-DATA. 03 ICE-CREAM-STORE-CODE PIC XXXXX. 03 ICE-CREAM-STORE-ADDRESS PIC X(20). 03 ICE-CREAM-STORE-CITY PIC X(20). 03 ICE-CREAM-STORE-STATE PIC XX. WORKING-STORAGE SECTION. 01 END-OF-FILE PIC X. PROCEDURE DIVISION. A000-BEGIN. OPEN INPUT FLAVORS. A010-SEQUENTIAL-READ. PERFORM A100-READ-INPUT UNTIL END-OF-FILE = "Y". A020-EOJ. DISPLAY "END OF JOB". STOP RUN. A100-READ-INPUT. READ FLAVORS AT END MOVE "Y" TO END-OF-FILE. IF END-OF-FILE NOT = "Y" DISPLAY ICE-CREAM-MASTER STOP "Type CONTINUE to display next master". |
Reading an Indexed File Randomly
Reading indexed records randomly involves the following:
To read the file randomly, the program must initialize either the primary key data name or the alternate key data name before reading the target record, and specify that data name in the KEY IS phrase of the READ statement.
The READ statement selects a specific record from an open file and makes it available to the program. The value of the primary or alternate key identifies the specific record. The system randomly reads the record identified by the KEY clause. If the I/O system does not find a valid record, the invalid key condition occurs, and the READ statement fails (see Chapter 7).
Example 6-33 reads an indexed file randomly, displaying its contents on the terminal.
Example 6-33 Reading an Indexed File Randomly |
---|
IDENTIFICATION DIVISION. PROGRAM-ID. INDEX04. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FLAVORS ASSIGN TO "DAIRY" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS ICE-CREAM-KEY. DATA DIVISION. FILE SECTION. FD FLAVORS. 01 ICE-CREAM-MASTER. 02 ICE-CREAM-KEY PIC XXXX. 02 ICE-CREAM-DATA. 03 ICE-CREAM-STORE-CODE PIC XXXXX. 03 ICE-CREAM-STORE-ADDRESS PIC X(20). 03 ICE-CREAM-STORE-CITY PIC X(20). 03 ICE-CREAM-STORE-STATE PIC XX. WORKING-STORAGE SECTION. 01 PROGRAM-STAT PIC X. 88 OPERATOR-STOPS-IT VALUE "1". PROCEDURE DIVISION. A000-BEGIN. OPEN I-O FLAVORS. PERFORM A020-INITIAL-PROMPT. IF OPERATOR-STOPS-IT PERFORM A005-TERMINATE. PERFORM A030-RANDOM-READ. PERFORM A025-SUBSEQUENT-PROMPTS UNTIL OPERATOR-STOPS-IT. DISPLAY "END OF JOB". STOP RUN. A020-INITIAL-PROMPT. DISPLAY "Do you want to see a store?". PERFORM A040-GET-ANSWER UNTIL PROGRAM-STAT = "Y" OR "y" OR "N" OR "n". IF PROGRAM-STAT = "N" OR "n" MOVE "1" TO PROGRAM-STAT. A025-SUBSEQUENT-PROMPTS. MOVE SPACE TO PROGRAM-STAT. DISPLAY "Do you want to see another store ?". PERFORM A040-GET-ANSWER UNTIL PROGRAM-STAT = "Y" OR "y" OR "N" OR "n". IF PROGRAM-STAT = "Y" OR "y" PERFORM A030-RANDOM-READ ELSE MOVE "1" TO PROGRAM-STAT. A030-RANDOM-READ. DISPLAY "Enter key". ACCEPT ICE-CREAM-KEY. PERFORM A100-READ-INPUT-BY-KEY. A040-GET-ANSWER. DISPLAY "Please answer Y or N" ACCEPT PROGRAM-STAT. A100-READ-INPUT-BY-KEY. READ FLAVORS KEY IS ICE-CREAM-KEY INVALID KEY DISPLAY "Record does not exist - Try again" NOT INVALID KEY DISPLAY "The record is: ", ICE-CREAM-MASTER. A005-TERMINATE. DISPLAY "terminated". |
Reading an Indexed File Dynamically
The READ statement has two formats, so it can select the next logical record (sequential access) or select a specific record (random access) and make it available to the program. In dynamic mode, the program can switch from using random access I/O statements to sequential access I/O statements, in any order and any number of times, without closing and reopening files. However, the program must use the READ NEXT statement to sequentially read an indexed file opened in dynamic mode.
Sequential processing need not begin at the first record of an indexed file. The START statement specifies the next record to be read sequentially, selects which key to use to determine the logical sort order, and repositions the file position indicator for subsequent I/O operations anywhere within the file.
A sequential read of a dynamic file is indicated by the NEXT phrase of the READ statement. A READ NEXT statement should follow the START statement since the READ NEXT statement reads the next record indicated by the file position indicator. Subsequent READ NEXT statements sequentially retrieve records until another START statement or random READ statement executes.
Example 6-34 processes an indexed file containing 26 records. Each record has a unique letter of the alphabet as its primary key. The program positions the file to the first record whose INPUT-RECORD-KEY is equal to the specified letter of the alphabet. The program's READ NEXT statement sequentially retrieves the remaining valid records in the file for display on the terminal.
Example 6-34 Reading an Indexed File Dynamically |
---|
IDENTIFICATION DIVISION. PROGRAM-ID. INDEX05. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT IND-ALPHA ASSIGN TO "ALPHA" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS INPUT-RECORD-KEY. DATA DIVISION. FILE SECTION. FD IND-ALPHA. 01 INPUT-RECORD. 02 INPUT-RECORD-KEY PIC X. 02 INPUT-RECORD-DATA PIC X(50). WORKING-STORAGE SECTION. 01 END-OF-FILE PIC X. PROCEDURE DIVISION. A000-BEGIN. OPEN I-O IND-ALPHA. DISPLAY "Enter letter" ACCEPT INPUT-RECORD-KEY. START IND-ALPHA KEY = INPUT-RECORD-KEY INVALID KEY DISPLAY "BAD START STATEMENT" NOT INVALID KEY PERFORM A100-GET-RECORDS THROUGH A100-GET-RECORDS-EXIT UNTIL END-OF-FILE = "Y" END-START. A010-END-OF-JOB. DISPLAY "END OF JOB". CLOSE IND-ALPHA. STOP RUN. A100-GET-RECORDS. READ IND-ALPHA NEXT RECORD AT END MOVE "Y" TO END-OF-FILE. IF END-OF-FILE NOT = "Y" DISPLAY INPUT-RECORD. A100-GET-RECORDS-EXIT. EXIT. |
On Alpha and I64, READ PRIOR retrieves from an Indexed file a record that logically precedes the one made current by the previous file access operation, if such a logically previous record exists. READ PRIOR can only be used with a file whose organization is INDEXED and whose access mode is DYNAMIC. The file must be opened for INPUT or I-O. Example 6-35 is an example of READ PRIOR in a program.
Example 6-35 Reading an Indexed File Dynamically, with READ PRIOR (Alpha, I64) |
---|
IDENTIFICATION DIVISION. PROGRAM-ID. READ_PRIOR. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F ASSIGN TO "READPR" ORGANIZATION IS INDEXED ACCESS IS DYNAMIC RECORD KEY IS K0 ALTERNATE RECORD IS K2 DUPLICATES. DATA DIVISION. FILE SECTION. FD F. 01 R. 02 K0 PIC X(3). 02 FILLER PIC X(5). 02 K2 PIC X(2). PROCEDURE DIVISION. P0. DISPLAY "***READ_PRIOR***". *+ * Indexed file creation: After this load, the indexed file * contains the following records : 0123456789, 1234567890, * 2345678990, and 9876543291 *+ OPEN OUTPUT F. MOVE "0123456789" TO R. WRITE R INVALID KEY DISPLAY "?1". MOVE "1234567890" TO R. WRITE R INVALID KEY DISPLAY "?2". MOVE "2345678990" TO R. WRITE R INVALID KEY DISPLAY "?3". MOVE "9876543291" TO R. WRITE R INVALID KEY DISPLAY "?4". CLOSE F. *+ * READ PREVIOUS immediately after file open for IO *+ OPEN I-O F. MOVE "000" TO K0. READ F PREVIOUS AT END GO TO P1 END-READ. DISPLAY "?5 " R. P1. CLOSE F. *+ * READ PREVIOUS after file open for IO, from a middle * record to beginning record on primary key. *+ OPEN I-O F. MOVE "2345678990" TO R. READ F INVALID KEY DISPLAY "?6" GO TO P2 END-READ. IF R NOT = "2345678990" THEN DISPLAY "?7 " R. READ F PREVIOUS AT END DISPLAY "?8" GO TO P2 END-READ. IF R NOT = "1234567890" THEN DISPLAY "?9 " R. READ F PREVIOUS AT END DISPLAY "?10" GO TO P2 END-READ. IF R NOT = "0123456789" THEN DISPLAY "?11 " R. READ F PREVIOUS AT END GO TO P2. DISPLAY "?12 " R. *+ * Multiple READ PREVIOUS on a display alternate key with * duplicates. *+ P2. MOVE "91" TO K2. READ F KEY K2 INVALID KEY DISPLAY "?13" GO TO P5 END-READ. R NOT = "9876543291" THEN DISPLAY "?14 " R. READ F PREVIOUS AT END DISPLAY "?15" GO TO P5 END-READ. IF R NOT = "2345678990" THEN DISPLAY "?16 " R. READ F PREVIOUS AT END DISPLAY "?17" GO TO P5 END-READ. IF R NOT = "1234567890" THEN DISPLAY "?18 " R. READ F PREVIOUS AT END DISPLAY "?19" GO TO P5 END-READ. IF R NOT = "0123456789" THEN DISPLAY "?20 " R. READ F PREVIOUS AT END GO TO P5. DISPLAY "?21 " R. P5. CLOSE F. DISPLAY "***END***". STOP RUN. <> |
Example 6-36 is another example of READ PRIOR. This example contrasts how duplicates are handled with a DESCENDING key and with READ PRIOR. Also, this example shows how to use START before initiating a sequence of either READ NEXT statements or READ PRIOR statements. This example highlights how to use START, if you switch between READ NEXT and READ PRIOR.
Example 6-36 Another Example of READ PRIOR (Alpha, I64) |
---|
***READ_PRIOR2*** Read ascending key a1 b2 c2 d2 e3 Read descending key e3 b2 c2 d2 a1 Read prior e3 d2 c2 b2 a1 ***END*** IDENTIFICATION DIVISION. PROGRAM-ID. READ_PRIOR2. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT OPTIONAL F1 ASSIGN TO "READPR" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS K1 = W2 ASCENDING WITH DUPLICATES ALTERNATE RECORD KEY IS K2 = W2 DESCENDING WITH DUPLICATES. DATA DIVISION. FILE SECTION. FD F1. 01 R1. 02 W1 PIC X. 02 W2 PIC X. PROCEDURE DIVISION. P0. DISPLAY "***READ_PRIOR2***". *+ * Indexed file creation. *- OPEN OUTPUT F1. MOVE "a1" TO R1. WRITE R1 INVALID KEY DISPLAY "?a1". MOVE "b2" TO R1. WRITE R1 INVALID KEY DISPLAY "?b2". MOVE "c2" TO R1. WRITE R1 INVALID KEY DISPLAY "?c2". MOVE "d2" TO R1. WRITE R1 INVALID KEY DISPLAY "?d2". MOVE "e3" TO R1. WRITE R1 INVALID KEY DISPLAY "?e3". CLOSE F1. *+ * Read using ascending key. *- OPEN INPUT F1. DISPLAY "Read ascending key". MOVE "0" TO W2. START F1 KEY IS GREATER THAN K1 INVALID KEY DISPLAY "?S1". PERFORM 5 TIMES READ F1 NEXT AT END DISPLAY "?R2" END-READ DISPLAY R1 END-PERFORM. CLOSE F1. *+ * Read using descending key. *- OPEN INPUT F1. DISPLAY "Read descending key". MOVE "4" TO W2. START F1 KEY IS GREATER THAN K2 INVALID KEY DISPLAY "?S2". PERFORM 5 TIMES READ F1 NEXT AT END DISPLAY "?R2" END-READ DISPLAY R1 END-PERFORM. *+ * READ PRIOR - note the difference in duplicate order from * Read with a descending key. *- DISPLAY "Read prior". MOVE "4" TO W2. START F1 KEY IS LESS THAN K1 INVALID KEY DISPLAY "?S3". PERFORM 5 TIMES READ F1 PRIOR AT END DISPLAY "?R3" END-READ DISPLAY R1 END-PERFORM. CLOSE F1. DISPLAY "***END***". STOP RUN. |
Previous | Next | Contents | Index |