Skip to content

SEARCH Statement

The SEARCH statement does a sequential or binary search of a table that is populated with data. The binary search (SEARCH ALL) statement requires that the data be pre-sorted.

Format 1

The Format 1 SEARCH statement reads through a table sequentially until a condition tests TRUE, or until the end of the table is reached.

       SEARCH search-table-1 [VARYING {index-1 ] 
              [ AT END statement-1 ] 
              { WHEN condition-1 {statement-2   } } ... 
                                 {NEXT SENTENCE }
              [ END-SEARCH ]

Syntax:

  1. search-tbl-n is a data item with an occurs clause, and an index. When used with the SEARCH ALL, a key is also required.
  2. index-n is a table index, and must be described with USAGE INDEX.
  3. statement-n is an imperative statement.

General Rules:

  1. The VARYING clause in not necessary if the table being SEARCH’ed contains an INDEXED BY clause.
  2. Index-1 defines the starting position in the table of the search. For a search of a table from beginning, to end, index-1 should be SET to 1 prior to the execution of the SEARCH statement, as follows:

    SET index-1 TO 1.
  3. The WHEN condition-1 phrase is tested for each instance of the table during the SEARCH. When condition-1 tests TRUE, statement-2 is executed, after which the SEARCH is terminated, and control passes to the next statement after the SEARCH statement.
  4. Where multiple WHEN condition phrases are listed consecutively, they will each be tested for each instance of the table during the SEARCH. When any WHEN condition phrase tests TRUE, the statement-list associated with it is executed, after which the SEARCH is terminated, and control passes to the next statement after the SEARCH statement.
  5. When the condition test is TRUE, the index stores the location in the table of the element that has been identified.
  6. The AT END condition is triggered when the entire table is SEARCH’ed and no WHEN condition phrase tests true. When the AT END condition is triggered, the statement-list associated with the AT END phrase is executed, the SEARCH is terminated, and control passes to the next statement after the SEARCH statement.

Format 2

The Format 2 SEARCH statement performs a binary search on a table that is sorted, and in which the sort-keys are described in the declaration of the table.

       SEARCH ALL search-table-1 
              [AT END statement-1 ] 
              WHEN {data-1 {IS EQUAL TO } { identifier-3 }} 
                           {IS =        } {literal-1      } 
                           {arithmetic-expression} {condition-1} 
              [AND {data-2 {IS EQUAL TO} { identifiier-4 } } ] ... 
                           { IS =      } { literal-2       } 
                           {arithmetic-expr } { condition-name-2 |} 
              {statement-2 }... 
              {NEXT SENTENCE } 
              [END-SEARCH ]

Syntax:

  1. search-tbl-n is a data item with an occurs clause, and an index. When used with the SEARCH ALL, a key is also required.
  2. data-n is a data item.
  3. identifier-n is a data element, literal, or data returned from a function call.
  4. literal-n is a character string.
  5. condition-n is a condition name described in a level 88 statement.
  6. statement-n is an imperative statement.

General Rules:

  1. index-1 does not need to be SET before the execution of a SEARCH ALL statement.
  2. The contents of a table that is the target of a SEARCH ALL statement must be pre-sorted. The manner in which the SEARCH ALL statement determines whether or not there is a match is to internally SET the index to a value that represents the midpoint of the table, and test the truth of the condition clause. After the condition-test, the SEARCH ALL statement limits the rest of its SEARCH to either the data elements above, or below the midpoint in the table. In this manner, it performs a binary search, and determines whether or not there is a match to the condition clause.
  3. The Format 2 SEARCH ALL statement only permits a single WHEN phrase.
  4. In order to get predictable results, the `WHEN phrase must identify a unique table entry.
  5. A condition test may be made up of multiple condition clauses that are connected with AND or OR.
  6. When the condition-test is TRUE, the index stores the location in the table of the element that has been identified.
  7. The VARYING syntax is not relevant, and not allowed on a SEARCH ALL statement.
  8. The AT END condition is triggered when the entire table is SEARCH’ed and no WHEN condition phrase tests true. When the AT END condition is triggered, the statement-list associated with the AT END phrase is executed, the SEARCH is terminated, and control passes to the next statement after the SEARCH statement.

Code Sample:

       IDENTIFICATION DIVISION. 
       PROGRAM-ID. SEARCH-1. 
       ENVIRONMENT DIVISION. 
       DATA DIVISION. 
       WORKING-STORAGE SECTION. 
       01 ALLSTAR-TABLE. 
              05 STAR-1     PIC X(30) VALUE "01HANK     AARON       BRAVES      ". 
              05 STAR-2     PIC X(30) VALUE "02YOGI     BERRA       YANKEES     ". 
              05 STAR-3     PIC X(30) VALUE "03RODNEY   CAREW       TWINS       ". 
              05 STAR-4     PIC X(30) VALUE "04JOE      DIMAGGIO    YANKEES     ". 
              05 STAR-5     PIC X(30) VALUE "05DENNIS   ECKERSLEY   AS          ". 
              05 STAR-6     PIC X(30) VALUE "06CARLTON  FISK        RED SOX     ". 
              05 STAR-7     PIC X(30) VALUE "07LOU      GEHRIG      YANKEES     ". 
              05 STAR-8     PIC X(30) VALUE "08ROGERS   HORNSBY     CARDINALS   ". 
              05 STAR-9     PIC X(30) VALUE "09REGGIE   JACKSON     YANKEES     ". 
              05 STAR-10    PIC X(30) VALUE "10SANDY    KOUFAX      DODGERS     ". 
              05 STAR-11    PIC X(30) VALUE "11TOMMY    LASORDA     DODGERS     ". 
              05 STAR-12    PIC X(30) VALUE "12EDDIE    MURRAY      ORIOLES     ". 
              05 STAR-13    PIC X(30) VALUE "13PHIL     NIEKRO      BRAVES      ".
              05 STAR-14    PIC X(30) VALUE "14JIM      PALMER      ORIOLES     ". 
              05 STAR-15    PIC X(30) VALUE "15BABE     RUTH        YANKEES     ". 
              05 STAR-16    PIC X(30) VALUE "16TOM      SEAVER      METS        ". 
              05 STAR-17    PIC X(30) VALUE "17BILL     VEECK       WHITE SOX   ". 
              05 STAR-18    PIC X(30) VALUE "19EARL     WEAVER      ORIOLES     ". 
              05 STAR-19    PIC X(30) VALUE "19BILLY    WILLIAMS    CUBS        ". 
              05 STAR-20    PIC X(30) VALUE "20CARL     YAZ         RED SOX     ".
      * 
       01 ALLSTAR-TBL REDEFINES ALLSTAR-TABLE. 
              05 STAR-TABLE OCCURS 20 TIMES 
                            ASCENDING KEY IS A-LAST-NAME 
                            INDEXED BY INDEX-1. 
                     10 A-NUMBER PIC 99. 
                     10 A-FIRST-NAME PIC X(8). 
                     10 A-LAST-NAME PIC X(10). 
                     10 A-TEAM PIC X(10). 

       77 DUMMY PIC X. 
       PROCEDURE DIVISION. 
       MAIN. 
      * SEARCH VARYING REQUIRES THE INDEX BE SET BEFORE BEGINNING 
              SET INDEX-1 TO1. 

              SEARCH STAR-TABLE VARYING INDEX-1 
                     AT END 
                            DISPLAY "END OF SEARCH!" LINE 11 COL 10 
                                   WHEN A-LAST-NAME(INDEX-1) = "KOUFAX" 
                                          DISPLAY A-FIRST-NAME(INDEX-1) LINE10COL10 
              END-SEARCH. 

      * SEARCH ALL REQUIRES THE TABLE BE SORTED ON A KEY FIELD 
      * NAMED IN THE DECLARATION. 
      * INDEX-1 IS AUTOMATICALLY INITIALIZED BY SEARCH ALL 

              SEARCH ALL STAR-TABLE 
                     AT END 
                            DISPLAY "END OF SEARCH ALL!" LINE 13 COL 10 
                                   WHEN A-LAST-NAME(INDEX-1) = "RUTH" 
                                          DISPLAY A-FIRST-NAME(INDEX-1) LINE 12 COL 10 
              END-SEARCH. 

              DISPLAY "SEARCH-1 FINISHED!" LINE 15 COL 10. 
              ACCEPT DUMMY LINE 15 COL 30. 
              STOP RUN.
Back to top