As you aware that SEARCH and SEARCH ALL both are COBOL reserved words. You can use these two to search COBOL internal tables or arrays. How to code these two and key differences is a question. This has been asked many times in interviews.
SEARCH in COBOL
SET DIV-IX TO 1.
SEARCH DIVISION-ENTRY
AT END
MOVE 8 TO ORG-SUB
WHEN ORD-RECIPIENT = DIVISION-CODE (DIV-IX)
SET ORG-SUB TO DIV-IX.
ADD 1 TO DIVISION-COUNT (ORG-SUB).
COMPUTE DIVISION-AMOUNT (ORG-SUB)
= DIVISION-AMOUNT (ORG-SUB) + (ORD-ITEM-COUNT * ORD-ITEM-RATE)
END-SEARCH
Search Vs Perform Varying in COBOL.
Search
- SEARCH can be used in COBOL program to search an internal table
- In SEARCH statement, always need to give AT END prior to WHEN
- We should not give NOT-WHEN or WHEN OTHER in SEARCH. The AT END will take care of this
- SEARCH does serial search on the table
- Another available options is : SEARCH internal-table VARYING index-name This you can use if you declared a table with multiple indexes. But the “index-name” will get priority, however order indexes are declared in a table.
PERFORM VARYING
PERFORM
VARYING DIV-IX
FROM 1 BY 1
UNTIL ORG-RECIPIENT = DIVISION-CODE (DIV-IX)
OR DIV-IX > 7
CONTINUE
END-PERFORM.
SET ORG-SUB TO DIV-IX.
ADD 1 TO DIVISION-COUNT (ORG-SUB).
COMPUTE DIVISION-AMOUNT (ORG-SUB)
= DIVISION-AMOUNT (ORG-SUB)
+ (ORD-ITEM-COUNT * ORD-ITEM-RATE).
When to use Search.
If there are more than ’50’ items in a Table, then, SEARCH is good.
SEARCH All in COBOL
- Table must be either in Ascending or Descending
- When clause can use only with “=”
- Always need to give in the order: WHEN test-field (DIV-IX) = receiver-field
- VARYING clause is not allowed in SEARCH ALL
- Only “AND” is allowed. The “OR” is NOT allowed
- If there are fewer item in a table “up to 25”, then, SEARCH ALL is good.
- SEARCH ALL -does binary search on the table
Related Posts
One thought
Comments are closed.