Currently trying to understand the Search verb while learning Cobol. Below is what I came up with when searching a ordinary sequential file and a indexed file. However I wonder, is this a good solution, or could it be improved / more efficient?
Code can also be viewed here: https://dpaste.com/HYZEWD8JH
IDENTIFICATION DIVISION.
PROGRAM-ID. Search.ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.SELECT AIRLINES ASSIGN TO AIRLINES.DAT
ORGANIZATION IS LINE SEQUENTIAL.SELECT AIRLINES-INDEXED ASSIGN TO
“/home/david/cobol/Search/AIRLINES-INDEXED.DAT”
ORGANIZATION IS INDEXED
RECORD KEY IS I-AIRLINE-ID.DATA DIVISION.
FILE SECTION.FD AIRLINES.
01 AIRLINE-RECORDS.
05 AIRLINE-ID PIC 99.
05 AIRLINE-NAME PIC X(15).FD AIRLINES-INDEXED.
01 AIRLINE-INDEXED.
05 I-AIRLINE-ID PIC 99.
05 I-AIRLINE-NAME PIC X(15).WORKING-STORAGE SECTION.
01 WS-AIRLINES.
05 WS-ENTRIES OCCURS 4 TIMES INDEXED BY X1.
10 WS-AIRLINE-ID PIC 99.
10 WS-AIRLINE-NAME PIC X(15).01 WS-I-AIRLINES.
05 WS-I-ENTRIES OCCURS 4 TIMES INDEXED BY X2.
10 WS-I-AIRLINE-ID PIC 99.
10 WS-I-AIRLINE-NAME PIC X(15).01 AIR-ID PIC 99 VALUE 02.
01 I-AIR-ID PIC 99 VALUE 03.
01 EOF-SWITCH PIC X VALUE “N”.PROCEDURE DIVISION.
000-INITIALIZE.
OPEN INPUT AIRLINES
AIRLINES-INDEXED
- SEARCH SEQUENTIAL FILE
PERFORM 100-READ-FILE
PERFORM 200-SEARCH-FILE
- SEARCH INDEXED FILE
PERFORM 150-READ-INDEXED-FILE
PERFORM 250-SEARCH-INDEXED-FILECLOSE AIRLINES
AIRLINES-INDEXEDSTOP RUN
.100-READ-FILE.
SET X1 TO 1
PERFORM VARYING X1 FROM 1 BY 1 UNTIL EOF-SWITCH = “Y”
READ AIRLINES
AT END
MOVE “Y” TO EOF-SWITCH
NOT AT END
MOVE AIRLINE-ID TO WS-AIRLINE-ID (X1)
MOVE AIRLINE-NAME TO WS-AIRLINE-NAME (X1).
150-READ-INDEXED-FILE.
MOVE “N” TO EOF-SWITCH
PERFORM VARYING X2 FROM 1 BY 1 UNTIL EOF-SWITCH = “Y”
READ AIRLINES-INDEXED
AT END
MOVE “Y” TO EOF-SWITCH
NOT AT END
MOVE I-AIRLINE-ID TO WS-I-AIRLINE-ID (X2)
MOVE I-AIRLINE-NAME TO WS-I-AIRLINE-NAME (X2).
200-SEARCH-FILE.SET X1 TO 1. SEARCH WS-ENTRIES AT END DISPLAY "NOT FOUND" WHEN WS-AIRLINE-ID (X1) = AIR-ID DISPLAY WS-AIRLINE-NAME (X1) END-SEARCH
.
250-SEARCH-INDEXED-FILE.SET X2 TO 1 SEARCH WS-I-ENTRIES AT END DISPLAY "NOT FOUND" WHEN WS-I-AIRLINE-ID (X2) = I-AIR-ID DISPLAY WS-I-AIRLINE-NAME (X2) END-SEARCH
.