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-n
is a data item with an occurs clause, and an index. When used with theSEARCH ALL
, a key is also required.index-n
is a table index, and must be described withUSAGE INDEX
.statement-n
is an imperative statement.
General Rules:
- The
VARYING
clause in not necessary if the table beingSEARCH
’ed contains anINDEXED BY
clause. 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 beSET
to 1 prior to the execution of theSEARCH
statement, as follows:SET index-1 TO 1.
- The
WHEN condition-1
phrase is tested for each instance of the table during theSEARCH
. Whencondition-1
testsTRUE
,statement-2
is executed, after which theSEARCH
is terminated, and control passes to the next statement after theSEARCH
statement. - Where multiple
WHEN
condition phrases are listed consecutively, they will each be tested for each instance of the table during theSEARCH
. When anyWHEN
condition phrase testsTRUE
, thestatement-list
associated with it is executed, after which theSEARCH
is terminated, and control passes to the next statement after theSEARCH
statement. - When the condition test is
TRUE
, the index stores the location in the table of the element that has been identified. - The
AT END
condition is triggered when the entire table isSEARCH
’ed and noWHEN
condition phrase tests true. When theAT END
condition is triggered, thestatement-list
associated with theAT END
phrase is executed, theSEARCH
is terminated, and control passes to the next statement after theSEARCH
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:
search-tbl-n
is a data item with an occurs clause, and an index. When used with theSEARCH ALL
, a key is also required.data-n
is a data item.identifier-n
is a data element, literal, or data returned from a function call.literal-n
is a character string.condition-n
is a condition name described in a level 88 statement.statement-n
is an imperative statement.
General Rules:
index-1
does not need to beSET
before the execution of aSEARCH ALL
statement.- The contents of a table that is the target of a
SEARCH ALL
statement must be pre-sorted. The manner in which theSEARCH ALL
statement determines whether or not there is a match is to internallySET
the index to a value that represents the midpoint of the table, and test the truth of the condition clause. After thecondition-test
, theSEARCH ALL
statement limits the rest of itsSEARCH
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. - The Format 2
SEARCH ALL
statement only permits a singleWHEN
phrase. - 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
AND
orOR
. - When the
condition-test
isTRUE
, the index stores the location in the table of the element that has been identified. - The
VARYING
syntax is not relevant, and not allowed on aSEARCH ALL
statement. - The
AT END
condition is triggered when the entire table isSEARCH
’ed and noWHEN
condition phrase tests true. When theAT END
condition is triggered, thestatement-list
associated with theAT END
phrase is executed, theSEARCH
is terminated, and control passes to the next statement after theSEARCH
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.