Here are the differences between Search and Search All in COBOL. Search uses a linear search algorithm, while Search All uses a binary search algorithm.

Search and Search All in COBOL
Photo by Rene Asmussen on Pexels.com

SEARCH Statement

Search in COBOL simplifies serial search. Moreover, consider these rules to use the “SEARCH” verb.

  • Search uses linear search
  • When searching a table, use the SEARCH statement with the AT END clause before any WHEN clauses. 
  • It is not necessary to include NOT-WHEN or WHEN OTHER clauses in the SEARCH statement since the AT END clause will handle any unmatched cases. 
  • If you have declared a table with multiple indexes, you can use the SEARCH statement with the VARYING keyword and an index name. In this case, the specified index will take priority, regardless of the order of the indexes.
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

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 Statement

When using the “SEARCH ALL” operation, keep in mind:

  • Search All uses binary search concept.
  • The table must be sorted in ascending or descending order.
  • Use the “WHEN” clause with the “=” operator to specify the order, like “WHEN test-field (DIV-IX) = receiver-field.”
  • You cannot use the “VARYING” clause with the “SEARCH ALL” operation.
  • Only the “AND” operator is allowed, and the “OR” operator is not allowed.
  • This operation works best when you have 25 rows in a table.
01  TABLE-A.
    05 TABLE-ENTRY OCCURS 90 TIMES
           ASCENDING KEY-1, KEY-2
           DESCENDING KEY-3
           INDEXED BY INDX-1.
       10 PART-1       PIC 99.
       10 KEY-1        PIC 9(5).
       10 PART-2       PIC 9(6).
       10 KEY-2        PIC 9(4).
       10 PART-3       PIC 9(18).
       10 KEY-3        PIC 9(5).
SEARCH ALL TABLE-ENTRY
  AT END
    PERFORM NOENTRY
  WHEN KEY-1 (INDX-1) = VALUE-1 AND
       KEY-2 (INDX-1) = VALUE-2 AND
       KEY-3 (INDX-1) = VALUE-3
    MOVE PART-1 (INDX-1) TO OUTPUT-AREA
END-SEARCH

Related Posts

One response

  1. […] Top differences of COBOL  Search and Search-All […]

    Like