0

I'm trying to understand the SEARCH verb...

Below I am reading sequential records from a file into a TABLE and then try to search it.. However it only matches when AIR-ID is 01 or 02, not 03.

I must be missing something fundamental, and been trying to look through various example with no luck.

   IDENTIFICATION DIVISION.
   PROGRAM-ID. Search.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
   
   SELECT AIRLINES ASSIGN TO "AIRLINES.DAT"
   ORGANIZATION IS LINE SEQUENTIAL.
   
   DATA DIVISION.
   FILE SECTION.
   
   FD AIRLINES.
   01  AIRLINE-RECORDS.
       05  AIRLINE-ID      PIC 99.
       05  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  AIR-ID    PIC 99  VALUE 04.
   01  EOF-SWITCH  PIC X   VALUE "N".
   
   PROCEDURE DIVISION.
   
   000-INITIALIZE.
       
   OPEN INPUT AIRLINES
   
   PERFORM 100-READ-FILE
   PERFORM 200-SEARCH-FILE
   .
       
   100-READ-FILE.
       READ AIRLINES INTO WS-AIRLINES
   .
       
   200-SEARCH-FILE.
       SET X1 TO 1.
           SEARCH WS-ENTRIES
               AT END 
                   DISPLAY "NOT FOUND"
               WHEN AIR-ID = WS-AIRLINE-ID (X1)
                   DISPLAY WS-AIRLINE-NAME (X1)
                   DISPLAY X1
           END-SEARCH
       .

AIRLINES.DAT

01airline01      
02airline02      
03airline03      
04airline04      

1 Answers1

0

Due to lack of answers, this is the best I could come up with:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. Search.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
   
   SELECT AIRLINES ASSIGN TO AIRLINES.DAT
   ORGANIZATION IS LINE SEQUENTIAL.
   
   DATA DIVISION.
   FILE SECTION.
   
   FD AIRLINES.
   01  AIRLINE-RECORDS.
       05  AIRLINE-ID      PIC 99.
       05  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  AIR-ID    PIC 99  VALUE 02.
   01  EOF-SWITCH  PIC X   VALUE "N".
   
   PROCEDURE DIVISION.
   
   000-INITIALIZE.
       
   OPEN INPUT AIRLINES
   
   PERFORM 100-READ-FILE
   PERFORM 200-SEARCH-FILE
   
   STOP 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)
                   DISPLAY AIRLINE-ID "-" AIRLINE-NAME
                 
       .
       
   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)
                   DISPLAY X1
           END-SEARCH
       .
  • Wow, no answer's on a coding related site on Sunday withing 16 hours... One thing this program really should do is to `CLOSE` the file after reading (I'd likely move that into the paragraph); If the record has identical types and length as in your case you may `MOVE` the complete entry instead of its items and instead of checking a switch that you set at end just `EXIT PERFORM` there. As this answer isn't accepted please comment what you "miss" here to solve your initial question. – Simon Sobisch Jan 17 '21 at 21:25
  • Appreciate your input and the sarcasm which was justified :) One question ... Wouldn't it be more "effective" in this case to match directly within the read/not at end, instead of using search? Using a simple IF WS-AIRLINE-ID = AIR-ID – Cult of Tranquility Jan 18 '21 at 21:51
  • If you only want to match a single entry or the file changes within the runtime of the program it is much more reasonable to use this direct in the `READ`. If you need to access it more than once then it is more effective to read everything into the table, then `SORT table` and then use `SEARCH ALL` multiple times. If the external file does not change that often you may want to "import" it into an `INDEXED` file once and then use its index for a direct single `READ` [but that wasn't this question, so I can't make an answer of it]. – Simon Sobisch Jan 20 '21 at 09:44