SEARCH ALL Statement
- SEARCH ALL statement performs binary search on tables (or arrays).
- It is more efficient for large tables, provided the table is sorted (ascending or descending) before the binary search (SEARCH ALL) applies.
Points to Note -
- INDEX should be declared on the table.
- INDEX Initialization not required before the SEARCH starts.
- SEARCH ALL can apply only on sorted tables.
Syntax -
SEARCH ALL table-name
[AT END statement-block-1]
WHEN relational-condition
statement-block-2|NEXT SENTENCE
...
[END-SEARCH]
Parameters -
- table-name - Name of the table.
- index-name - The name of the index associated with the table.
- AT END ... - A statement or set of statements to be executed if the search reaches the end of the table without finding a match.
- WHEN ... - Specifies a condition that's evaluated for each table element. When the condition is true, the statements following the WHEN clause are executed.
- END-SEARCH ... - It is used to end the scope of the SEARCH statement and is not required when SEARCH statement ended with period.
Search Process -
Initialization -
The search begins by setting (automatically) the index to the midpoint of the table. If the table has an even number of entries, COBOL will take the lower half.
Sequential validation -
SEARCH checks the WHEN condition for the current table entry (based on the index value).
- If the current table entry matches the search criteria, the search is successful, and the associated imperative statements for the WHEN condition are executed.
- If the current table entry is less than the search criteria (for an ascending table), the first half of the table is effectively discarded. The index is now set to the midpoint of the remaining half, and the process returns to step 2.
- If the current table entry is greater than the search criterion (for an ascending table), the second half of the table is effectively discarded. The index is adjusted to the midpoint of the first half, and the search continues from step 2.
End of Table -
If the end of the table is reached without any WHEN condition being satisfied, the AT END clause (if present) will be executed.
End of SEARCH -
Once the SEARCH process is completed (either a matching entry was found, or the table's end was reached without a match), the execution continues with the statement immediately following the END-SEARCH or after the last WHEN clause if END-SEARCH is not coded.
Practical Example -
Scenario - Let us assume we have a employee table for an organization with all active employee numbers (sorted) and we are trying to searching for exmployee E0004 existance. If the employee found, we should display "Employee found". Otherwise, display "Employee not found".
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
WORKING-STORAGE SECTION.
01 WS-ORG.
03 WS-EMPLOYEE OCCURS 6 TIMES
INDEXED BY IDX-EMP.
05 WS-EMPLOYEE-NUM PIC X(05).
...
PROCEDURE DIVISION.
* Initializing table with active employee details
* every 5 characters represents one employee number.
MOVE 'E0001E0002E0004E0005E0007'
TO WS-ORG.
* Initializing index
SET IDX-EMP TO 1.
* Search table using index
SEARCH ALL WS-EMPLOYEE
AT END DISPLAY "Employee not found"
WHEN WS-EMPLOYEE-NUM(IDX-EMP) = 'E0004'
DISPLAY "Employee found"
END-SEARCH.
...
Output -
Employee found