Welcome! In previous lessons, you've learned how to create, write to, read from, and append records to sequential files in COBOL. In this lesson, we are taking a deeper dive into a more powerful concept: handling indexed files. Mastering indexed files will enable you to perform faster searches, updates, and deletions based on key fields, making your programs more efficient and scalable.
In this lesson, you will learn how to create and manipulate indexed files in COBOL. Indexed files allow you to access records randomly, which is a big advantage over sequential files when dealing with large datasets. For this lesson, we will use a practical example where you write a customer record to an indexed file and then retrieve it based on an account number.
First we define the file structure and environment settings:
cobol1IDENTIFICATION DIVISION. 2PROGRAM-ID. IndexedFileDemo. 3ENVIRONMENT DIVISION. 4INPUT-OUTPUT SECTION. 5FILE-CONTROL. 6 SELECT IndexedFile ASSIGN TO 'customers.idx' 7 ORGANIZATION IS INDEXED 8 ACCESS MODE IS RANDOM 9 RECORD KEY IS Account-Number. 10DATA DIVISION. 11FILE SECTION. 12FD IndexedFile. 1301 Customer-Record. 14 05 Customer-Name PIC X(30). 15 05 Account-Number PIC 9(10). 16 05 Account-Balance PIC 9(8)V99.
In the above code snippet, we define an indexed file named IndexedFile
with the ORGANIZATION IS INDEXED
clause. The RECORD KEY IS Account-Number
clause specifies that the Account-Number
field will be used as the key for random access.
Next, we write a customer record to the indexed file:
cobol1PROCEDURE DIVISION. 2 OPEN OUTPUT IndexedFile. 3 MOVE "Michael Johnson" TO Customer-Name. 4 MOVE 8765432109 TO Account-Number. 5 MOVE 3200.25 TO Account-Balance. 6 WRITE Customer-Record. 7 CLOSE IndexedFile.
In the above code, we open the indexed file in output mode and write a customer record with the name "Michael Johnson," account number 8765432109
, and account balance 3200.25
.
Then we read the customer record based on the account number:
cobol1 MOVE " " TO Customer-Name. 2 MOVE 0 TO Account-Balance. 3 4 OPEN INPUT IndexedFile. 5 READ IndexedFile 6 INTO Customer-Record 7 KEY IS Account-Number 8 INVALID KEY 9 DISPLAY "Record not found." 10 NOT INVALID KEY 11 DISPLAY "Customer Name: " Customer-Name 12 DISPLAY "Account Number: " Account-Number 13 DISPLAY "Account Balance: " Account-Balance 14 END-READ. 15 CLOSE IndexedFile. 16 17 STOP RUN.
In this code snippet, we open the indexed file in input mode and read the customer record based on the account number 8765432109
. If the record is found, we display the customer name, account number, and account balance. Otherwise, we display a message indicating that the record was not found.
If we specify an invalid account number in the READ
statement with KEY IS <invalid key>
, the INVALID KEY
clause will be executed, and the message "Record not found"
will be displayed.
Let's note, that in the KEY IS
clause, we have to specify the key field exactly as it is defined in the FILE-CONTROL
section of the file with the RECORD KEY
clause - in this case, the key field is Account-Number
. For example KEY IS Some-Variable
will not work, even if it has the same value as Account-Number
. Similarly something like KEY IS 8765432109
will not work, as it is not a valid key field.
Handling indexed files efficiently is crucial for applications that require quick access to data, such as customer information systems and financial transactions. Indexed files allow for:
- Faster Data Retrieval: Immediate access to specific records using keys.
- Efficient Updates and Deletions: Modify or remove records without scanning the entire file.
- Scalability: Handle larger datasets with improved performance compared to sequential file operations.
By mastering indexed files, you'll significantly enhance the performance and scalability of your COBOL applications. Whether you are working in the banking sector, payroll systems, or any application needing quick data retrieval, knowledge of indexed files is indispensable.
Does this sound interesting? Let's move on to the practice section where you can implement what you've learned and see indexed files in action!