SEARCHING A TABLE  - IBM Mainframe

VS COBOL II provides two search techniques for tables: serial and binary. To perform serial searches:

  • Use PERFORM ... VARYING with subscripting or indexing
  • Use SEARCH and indexing

To perform binary searches use indexing and the SEARCH ALL statement.

Serial Search, Using SEARCH Statement

Use the SEARCH statement to perform a serial search beginning at the current index setting. (You can use the SET statement to modify the index setting before using the SEARCH statement.) The conditions in the WHEN option are evaluated in the order in which they are written.

  • If none of the conditions are satisfied, the index is increased to correspond to the next table element, and the WHEN conditions are again evaluated.
  • If one of the WHEN condition is satisfied, the search ends; the index remains pointing to the table element that satisfied the condition.
  • When the entire table has been searched and no conditions were met, the AT END imperative statement is executed, if there is one. If you do not use the AT END option, control passes to the next statement.

Only one level of a table (a table element) can be referenced with one SEARCH statement. SEARCH statements can be nested to search multiple levels of a table. However, SEARCH statements can be nested only if you delimit each nested SEARCH statement with END-SEARCH. The WHEN condition must be followed by an imperative statement; the SEARCH statement is an imperative statement only when it is delimited by END-SEARCH.

It is important to know if the found condition comes after some intermediate point in the table element. You can speed up the SEARCH by using the SET statement to set the index to begin the search after that point. Arranging the table so that the data used most often is at the beginning also enables more efficient serial searching. If the table is large and is presorted, a binary search is more efficient.

Binary Search, Using SEARCH ALL

When you use SEARCH ALL to perform a binary search, you do not need to set the index before you begin. The index used is always the one associated with the first index-name in the OCCURS clause, and it varies during execution to maximize the search efficiency.

To use the SEARCH ALL statement, your table must be ordered on the KEY(S) specified in the OCCURS clause. You can specify any KEY in the WHEN condition, but all preceding data-names in the KEY option must also be tested. The test must be an equal-to condition, and the KEY data-name must be either the subject of the condition or the name of a conditional variable with which the tested condition-name is associated. The WHEN condition can also be a compound condition, formed from one of the simple conditions listed above, with AND as the only logical connective. The KEY and its object of comparison must be compatible, as stated in the relation test rules. For example, a table defined like this:

Binary Search, Using SEARCH ALL

Binary Search, Using SEARCH ALL

These instructions will execute a search on the given table that contains 90 elements of 40 bytes and 3 keys. The primary and secondary keys (KEY-1 and KEY-2) are in ascending order, but the least significant key (KEY-3) is in descending order. If an entry is found in which three keys are equal to the given values (VALUE-1. VALUE-2, and VALUE-3), PART-1 of that entry will be moved to OUTPUT-AREA. If the matching keys are not found in any of the entries in TABLEA, the NOENTRY routine is performed.


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

IBM Mainframe Topics