Compaq COBOL AAQ2G1FTK User Manual

Page 139

Advertising
background image

Handling Tables

4.3 Accessing Table Elements

4.3.8.1 Implementing a Sequential Search

The SEARCH statement allows you to perform a sequential search of a table. The
OCCURS clause of the table description entry must contain the INDEXED BY
phrase. If more than one index is specified in the INDEXED BY phrase, the first
index is the controlling index for the table search unless you specify otherwise in
the SEARCH statement.

The search begins at the current index setting and progresses through the
table, checking each element against the conditional expression. The index is
incremented by 1 as each element is checked. If the conditional expression is
true, the associated imperative statement executes; otherwise, program control
passes to the next procedural sentence. This terminates the search, and the index
points to the current table element that satisfied the conditional expression.

If no table element is found that satisfies the conditional expression, program
control passes to the AT END exit path; otherwise, program control passes to the
next procedural sentence.

You can use the optional VARYING phrase of the SEARCH statement by
specifying any of the following:

VARYING index name associated with table search

VARYING index data item or integer data item

VARYING index name not associated with table search

Regardless of which method you use, the index specified in the INDEXED BY
phrase of the table being searched is incremented. This controlling index, when
compared against the allowable number of occurrences in the table, dictates the
permissible search range. When the search terminates, either successfully or
unsuccessfully, the index remains at its current setting. At this point, you can
reference the data in the table element pointed to by the index, unless the AT
END condition is true. If the AT END condition is true, and if the

-check

flag (on

Tru64 UNIX systems) or the /CHECK qualifier (on OpenVMS Alpha systems) has
been specified, the compiler issues a run-time error message indicating that the
subscript is out of range.

When you vary an index associated with the table being searched, the index
name can be any index you specify in the INDEXED BY phrase. It becomes the
controlling index for the search and is the only index incremented. Example 4–18
and Example 4–20 show how to vary an index other than the first index.

When you vary an index data item or an integer data item, either the index data
item or the integer data item is incremented. The first index name you specify
in the INDEXED BY phrase of the table being searched becomes the controlling
index and is also incremented. The index data item or the integer data item you
vary does not function as an index; it merely allows you to maintain an additional
pointer to elements within a table. Example 4–18 and Example 4–21 show how
to vary an index data item or an integer data item.

When you vary an index associated with a table other than the one you are
searching, the controlling index is the first index you specify in the INDEXED
BY phrase of the table you are searching. Each time the controlling index is
incremented, the index you specify in the VARYING phrase is incremented. In
this manner, you can search two tables in synchronization. Example 4–18 and
Example 4–22 show how to vary an index associated with a table other than the
one you are searching.

Handling Tables 4–17

Advertising