Good way Searching Indexed and Sequential files?

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: dpaste: 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-FILE

CLOSE AIRLINES
AIRLINES-INDEXED

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)

.
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

.

Generally I think both searches would both work about the same. Since you are using an indexed file for the second search, the items will be in sequence, so it is possible this search may be slightly faster. When the items are in sequence and there are no duplicates, you should use the “SEARCH ALL” format as it performs a binary search of the table rather than having to examine EACH table entry until either the END is encountered or the item is found. I think using the “SEARCH ALL” format would be the better and faster method to use. Finally I don’t see the variables WS-ENTRIES & WS-I-ENTRIES anywhere.

The SEARCH statement is used in a program to search for an item in an indexed table previously loaded into memory. In your code, the same search is done and has nothing to do with the organization of the file since it is loaded into memory in a table. To improve the search, you can use SEARCH ALL (binary search) on condition that you have declared the sorted table (on ASCENDING or DESCENDING key).

IDENTIFICATION DIVISION.
PROGRAM-ID. Search.
*-------------------------------------------------------------------

  • Exemples de recherches dans une table indexée
    *-------------------------------------------------------------------
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
    DATA DIVISION.
    FILE SECTION.

WORKING-STORAGE SECTION.

01 WS-ZON-TRAV.
05 WS-MOIS-CHERCHE PIC 99.
05 WS-JOUR-CHERCHE PIC 99.

01 WS-INI-MOIS.
05 FILLER PIC X(12) VALUE '01JANVIER '.
05 FILLER PIC X(12) VALUE '02FEVRIER '.
05 FILLER PIC X(12) VALUE '03MARS '.
05 FILLER PIC X(12) VALUE '04AVRIL '.
05 FILLER PIC X(12) VALUE '05MAI '.
05 FILLER PIC X(12) VALUE '06JUIN '.
05 FILLER PIC X(12) VALUE '07JUILLET '.
05 FILLER PIC X(12) VALUE '08AOUT '.
05 FILLER PIC X(12) VALUE '09SEPTEMBRE '.
05 FILLER PIC X(12) VALUE '10OCTOBRE '.
05 FILLER PIC X(12) VALUE '11NOVEMBRE '.
05 FILLER PIC X(12) VALUE ‘12123456789012’.

01 WS-TAB-MOIS.
05 WS-LIG-MOIS OCCURS 12 TIMES INDEXED BY IND-MOIS.
10 WS-NUM-MOIS PIC 99.
10 WS-LIB-MOIS PIC X(10).

01 WS-INI-JOUR.
05 FILLER PIC X(12) VALUE '01LUNDI '.
05 FILLER PIC X(12) VALUE '02MARDI '.
05 FILLER PIC X(12) VALUE '03MERCREDI '.
05 FILLER PIC X(12) VALUE '04JEUDI '.
05 FILLER PIC X(12) VALUE '05VENDREDI '.
05 FILLER PIC X(12) VALUE '06SAMEDI '.
05 FILLER PIC X(12) VALUE '07DIMANCHE '.

01 WS-TAB-JOUR.
05 WS-LIG-JOUR OCCURS 12 TIMES ASCENDING NUM-JOUR INDEXED BY IND-JOUR.
10 WS-NUM-JOUR PIC 99.
10 WS-LIB-JOUR PIC X(10).

PROCEDURE DIVISION.

000-INITIALIZE.

  • Chargement des tables MOIS et JOUR
    MOVE WS-INI-MOIS TO WS-TAB-MOIS.
    MOVE WS-INI-JOUR TO WS-TAB-JOUR.

  • Initialisation des valeurs MOIS et JOUR a chercher
    MOVE 08 TO WS-MOIS-CHERCHE.
    MOVE 04 TO WS-JOUR-CHERCHE.

PERFORM 100-SEARCH-MOIS.

PERFORM 200-SEARCH-JOUR.

STOP RUN.

*-----------------------------------

  • Recherche libellé mois
    *-----------------------------------

100-SEARCH-MOIS SECTION.
*------------------------------------------

SET IND-MOIS TO 1.

SEARCH WS-LIG-MOIS
AT END
DISPLAY ‘MOIS NON TROUVE’
WHEN WS-NUM-MOIS (IND-MOIS) = WS-MOIS-CHERCHE
DISPLAY 'Mois : ’ WS-LIB-MOIS (IND-MOIS)
END-SEARCH.

100-SEARCH-MOIS-END.
EXIT.

*-----------------------------------

  • Recherche libellé jour
    *-----------------------------------

200-SEARCH-JOUR SECTION.
*-------------------------------------------

SEARCH ALL WS-LIG-JOUR
AT END
DISPLAY ‘JOUR NON TROUVE’
WHEN WS-NUM-JOUR (IND-JOUR) = WS-JOUR-CHERCHE
DISPLAY 'Jour : ’ WS-LIB-JOUR (IND-JOUR)
END-SEARCH.

200-SEARCH-JOUR-END.
EXIT.

Thanks a lot for sharing your advice