SEARCH Statement COBOL

The SEARCH statement is used to search a table for a table element that satisfies the specified condition and to adjust the associated index-name to indicate that table element.

Format 1:Search (Serial)

Search (Serial)

Format 2:Search All (Binary)

Search All (Binary)

In both Formats 1 and 2, identifier-1 must not be subscripted or reference modified,but its description must contain an OCCURS clause with an INDEXED BY phrase.The description of identifier-1 in Format 2 must also contain the KEY IS phrase in its OCCURS clause.

identifier-2, when specified, must be described as USAGE IS INDEX or as a numeric elementary item without any positions to the right of the assumed decimal point.

In Format 1, condition-1 may be any conditional expression.

In Format 2, all referenced condition-names must be defined as having only a single value.The data-name associated with a condition-name must appear in the KEY phrase of the OCCURS clause of identifier-1.Each data-name may be qualified.Each data-name must be subscripted by the first index-name associated with identifier-1 along with other indexes or literals as required, and must be referenced in the KEY phrase of the OCCURS clause of identifier-1. identifier-3 , identifier-4 , or identifiers specified in arithmetic-expr-1 or arithmetic-expr-2 must not be referenced in the KEY phrase of the OCCURS clause of identifier-1 or be subscripted by the first index-name associated with identifier-1.

In Format 2, when multiple keys are defined and a data-name in the KEY phrase of the OCCURS clause of identifier-1 is referenced, or when a condition-name associated with a data-name in the KEY phrase of the OCCURS clause of identifier-1 is referenced, all preceding data-names in the KEY phrase of the OCCURS clause of identifier-1 or their associated condition-names must also be referenced.

General Rules

The following general rules apply to the SEARCH statement:

  1. If Format 1 is used, a serial type of search operation takes place, starting at the current index setting.
    • If, at the start of the execution of the search, the index-name associated with identifier-1 contains a value that corresponds to an occurrence number that is greater than the highest permissible occurrence number for identifier-1, the search is terminated immediately.Then, if the AT END phrase is specified, imperative-statement-1 is executed; if the AT END phrase is not specified, control passes to the end of the SEARCH statement.
    • If, at the start of the execution of the search, the index-name associated with identifier-1 contains a value that corresponds to an occurrence number that is not greater than the highest permissible occurrence number for identifier-1, the SEARCH statement operates by evaluating the conditions in the WHEN phrases in the order in which they are written, making use of the index settings to determine the occurrence of those items to be tested.If none of the conditions are satisfied, the index-name for identifier-1 is incremented, and the process is repeated unless the index is out of range (in which case the search terminates as indicated in rule 1a).If one of the conditions is satisfied, the search terminates immediately and control passes to the imperative statement associated with that condition, if present, or if the NEXT SENTENCE phrase is associated with that condition, the control passes to the next executable sentence.
  2. The index-name remains set at the value that causes the condition to be satisfied.

  3. In a Format 2 search, the results of the SEARCH ALL operation are predictable only when both of these conditions exist:
    • The data in the table is ordered in the same manner as described in the ASCENDING/DESCENDING KEY clause associated with the description of identifier-1.
    • The contents of the key (or keys) referenced in the WHEN clause are sufficient to identify a unique table element.
  4. If Format 2 of the SEARCH statement is used, a binary search technique is applied. The value of the index-name for identifier-1 is varied in alternating directions and in progressively smaller steps until either a value is found for which all the conditions of the WHEN phrase are satisfied or it is determined that no value allows all of the conditions to be satisfied. In the latter case, control is passed to imperative-statement-1 in the AT END phrase, if specified, or to the end of the SEARCH statement if there is no AT END phrase; in either case, the final setting of the index is not predictable.If a setting of the index is found for which all of the conditions are satisfied, control passes to imperative-statement-2, if specified, or if the NEXT SENTENCE phrase is specified, to the next executable sentence; in either case, the final setting of the index is the one for which the conditions are all satisfied.Regardless of the outcome of the SEARCH statement, the initial setting of the index is not significant.
  5. After execution of imperative-statement-1, imperative-statement-2, and so forth, that does not contain an explicit transfer of control, control passes to the end of the SEARCH statement.
  6. In Format 2, the index-name that is used for the search operation is the first (or only) index-name that appears in the INDEXED BY phrase of identifier-1.Any other index-names for identifier-1 remain unchanged.
  7. In Format 1, if the VARYING phrase is not used, the index-name that is used for the search operation is the first (or only) index-name that appears in the INDEXED BY phrase of identifier-1.Any other index-names for identifier-1 remain unchanged.
  8. In Format 1, if the VARYING index-name-1 phrase is specified, and if index-name-1 appears in the INDEXED BY phrase in the OCCURS clause referenced by identifier-1, that index-name is used for this search.If this is not the case, or if the VARYING identifier-2 phrase is specified, the first (or only) index-name given in the INDEXED BY phrase in the OCCURS clause referenced by identifier-1 is used for the search.In addition, the following operations occur:
    • If the VARYING index-name-1 phrase is used, and if index-name-1 appears in the INDEXED BY phrase in the OCCURS clause referenced by another table entry, the occurrence number represented by index-name-1 is incremented by the same amount as, and at the same time as, the occurrence number represented by the index-name associated with identifier-1 is incremented.
    • If the VARYING identifier-2 phrase is specified, and identifier-2 is an index data item, the data item referenced by identifier-2 is incremented by the same amount as, and at the same time as, the index associated with identifier-1 is incremented.If identifier-2 is not an index data item, the data item referenced by identifier-2 is incremented by the value 1 at the same time as the index referenced by the index-name associated with identifier-1 is incremented.
  9. If identifier-1 references a data item subordinate to a data item that contains an OCCURS clause, an index-name must be associated with each dimension of the table through the INDEXED BY phrase of the OCCURS clause.Only the setting of the index-name associated with identifier-1 (and the data item identifier-2 or index-name-1, if present) is modified by the execution of the SEARCH statement. To search a multidimensional table it is necessary to execute a SEARCH statement several times.Prior to each execution of a SEARCH statement, SET statements must be executed whenever index-names must be adjusted to appropriate settings.

A representation of the action of a Format 1 SEARCH statement containing two WHEN phrases is shown in Figure.

SEARCH Statement

SEARCH Statement

SEARCH Statement Examples


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics