SEARCH Statement
SEARCH is used to perform a linear search on the table (also known as an array) for a specific item. It works with tables that have the OCCURS clause and are INDEXED BY an index.
SEARCH scans the table elements one by one until a match is found or until the end of the table is reached. This SEARCH is called sequential search, serial search, linear search, or simple search.
Pints to Note -
- INDEX should be declared on the table.
- INDEX should be initialized before the SEARCH starts.
- SEARCH can apply on both sorted or unsorted tables.
- If SEARCH is used on multi-dimensional arrays, then the index value should set for all dimensions.
Syntax -
SEARCH table-name
VARYING index-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 at the current value of the index used in the SEARCH statement. Typically, before the SEARCH statement, we would initialize this index to a starting value, often the first occurrence in the table.
Sequential validation -
SEARCH checks the WHEN condition for the current table entry (based on the index value).
- If the WHEN condition is satisfied -
- The statements following that particular WHEN clause are executed.
- The search terminates (unless there is another WHEN condition to evaluate).
- If the WHEN condition is not satisfied -
- The index is incremented by 1, pointing to the next table entry.
- The process goes back to step 2, and the next table entry's WHEN condition is evaluated.
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 (unsorted) 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 'E0005E0002E0004E0001E0007'
TO WS-ORG.
* Initializing index
SET IDX-EMP TO 1.
* Search table using index
SEARCH WS-EMPLOYEE VARYING IDX-EMP
AT END DISPLAY "Employee not found"
WHEN WS-EMPLOYEE-NUM(IDX-EMP) = 'E0004'
DISPLAY "Employee found"
END-SEARCH.
...
Output -
Employee found