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:
search-tbl-nis a data item with an occurs clause, and an index. When used with theSEARCH ALL, a key is also required.index-nis a table index, and must be described withUSAGE INDEX.statement-nis an imperative statement.
General Rules:
- The
VARYINGclause in not necessary if the table beingSEARCH’ed contains anINDEXED BYclause. Index-1defines the starting position in the table of the search. For a search of a table from beginning, to end,index-1should beSETto 1 prior to the execution of theSEARCHstatement, as follows:SET index-1 TO 1.- The
WHEN condition-1phrase is tested for each instance of the table during theSEARCH. Whencondition-1testsTRUE,statement-2is executed, after which theSEARCHis terminated, and control passes to the next statement after theSEARCHstatement. - Where multiple
WHENcondition phrases are listed consecutively, they will each be tested for each instance of the table during theSEARCH. When anyWHENcondition phrase testsTRUE, thestatement-listassociated with it is executed, after which theSEARCHis terminated, and control passes to the next statement after theSEARCHstatement. - When the condition test is
TRUE, the index stores the location in the table of the element that has been identified. - The
AT ENDcondition is triggered when the entire table isSEARCH’ed and noWHENcondition phrase tests true. When theAT ENDcondition is triggered, thestatement-listassociated with theAT ENDphrase is executed, theSEARCHis terminated, and control passes to the next statement after theSEARCHstatement.
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:
search-tbl-nis a data item with an occurs clause, and an index. When used with theSEARCH ALL, a key is also required.data-nis a data item.identifier-nis a data element, literal, or data returned from a function call.literal-nis a character string.condition-nis a condition name described in a level 88 statement.statement-nis an imperative statement.
General Rules:
index-1does not need to beSETbefore the execution of aSEARCH ALLstatement.- The contents of a table that is the target of a
SEARCH ALLstatement must be pre-sorted. The manner in which theSEARCH ALLstatement determines whether or not there is a match is to internallySETthe index to a value that represents the midpoint of the table, and test the truth of the condition clause. After thecondition-test, theSEARCH ALLstatement limits the rest of itsSEARCHto 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. - The Format 2
SEARCH ALLstatement only permits a singleWHENphrase. - In order to get predictable results, the `WHEN phrase must identify a unique table entry.
- A condition test may be made up of multiple condition clauses that are connected with
ANDorOR. - When the
condition-testisTRUE, the index stores the location in the table of the element that has been identified. - The
VARYINGsyntax is not relevant, and not allowed on aSEARCH ALLstatement. - The
AT ENDcondition is triggered when the entire table isSEARCH’ed and noWHENcondition phrase tests true. When theAT ENDcondition is triggered, thestatement-listassociated with theAT ENDphrase is executed, theSEARCHis terminated, and control passes to the next statement after theSEARCHstatement.
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.