COBOL “Search” and “Search all” differences

Grow your career with these IT Jobs
[Grow your career with these IT Jobs]
As you aware SEARCH and SEARCH ALL both are COBOL reserved words. You can use these two to search COBOL tables. How to code these two and key differences is a question. This has been asked many times in interviews.

SEARCH:

  1. SEARCH can be used in COBOL program to search an internal table  –

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

  1. In SEARCH statement, always need to give AT END prior to WHEN
  2. We should not give NOT-WHEN or WHEN OTHER in SEARCH. The AT END will take care of this

  3. SEARCH does serial search on the table

  4. Another available options is : SEARCH internal-table VARYING index-name

a. 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 option you can use to search a table

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).

  • If there are more than ’50’ items in a Table, then, SEARCH is good.

SEARCH ALL:

  1. Table must be either in Ascending or Descending
  2. When clause can use only with “=”
  3. Always need to give in the order: WHEN test-field (DIV-IX) = receiver-field
  4. VARYING clause is not allowed in SEARCH ALL
  5. Only “AND” is allowed. The “OR” is NOT allowed
  6. If there are fewer item in a table “up to 25”, then, SEARCH ALL is good.
  7. SEARCH ALL -does binary search on the table
Advertisements

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.