COBOL - SEARCH ALL Statement
- Search All is a binary search method, which is used to find elements inside the table.
- Table must be in sorted order for Search All option.
- The index does not require initialization.
- The outcome of SEARCH ALL is either a yes or a no. Hence we can not code multiple search conditions when using SEARCH ALL function.
- In binary search, the table is divided into two halves and it determines in which half the searched element is present. This process repeats till the element is found or the end is reached.
Syntax:
SEARCH ALL identifier-1
AT END imperative statement-1
WHEN equal-condition-1 and equal-condition-2
{imperative statement-2}
{NEXT SENTENCE}
END-SEARCH
|
Example:
IDENTIFICATION DIVISION.
PROGRAM-ID. CBLHELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-RECORD OCCURS 10 TIMES ASCENDING KEY IS WS-EMP-ID INDEXED BY I.
10 WS-EMP-ID PIC 9(2).
10 WS-NAME PIC A(3).
PROCEDURE DIVISION.
MOVE '00ABC11DEF22GHI33JKL44MNO55PQR' TO WS-TABLE.
SEARCH ALL WS-RECORD
AT END DISPLAY 'INVALID EMP ID(RECORD NOT FOUND IN TABLE)'
WHEN WS-EMP-ID(I) = 33
DISPLAY 'RECORD FOUND '
DISPLAY WS-EMP-ID(I)
DISPLAY WS-NAME(I)
END-SEARCH.
STOP RUN.
|
OUTPUT:
When you compile and execute the above program, it produces the following result −
If WS-EMP-ID '33' is not found in WS-TABLE table, then the output of the above program is "INVALID EMP ID(RECORD NOT FOUND IN TABLE)".
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!