HP 3000 Manuals

SEARCH Statement [ HP COBOL II/XL Reference Manual ] MPE/iX 5.0 Documentation


HP COBOL II/XL Reference Manual

SEARCH Statement 

The SEARCH statement searches a table for an element that satisfies some
condition, and sets the table's index name to the value of the occurrence
number of the element that was found.

Syntax 

The two formats of the SEARCH statement are shown below:

[]
Parameters identifier-1 name of a table. It must not be subscripted or indexed, and must also contain an OCCURS and an INDEXED BY clause in its description. If used in format 2, it must also contain a KEY IS phrase in its OCCURS clause. identifier-2 if used, must be described either as USAGE IS INDEX, or as a numeric elementary item with no positions to the right of the assumed decimal point. condition-1 and may be any condition as described under the condition-2 heading, "Conditional Expressions", in Chapter 8 . condition-name-1 and condition names whose descriptions (level 88) condition-name-2 list only a single value. The data name associated with a condition name must appear in the KEY IS clause of identifier-1. data-name-1 and bmay each be qualified, and each must be indexed data-name-2 by the first index name associated with identifier-1, as well as any other indices or literals, as required. Each must also be referenced in the KEY IS clause of identifier-1. identifier-3 and must not be referenced in the KEY IS clause of identifier-4 identifier-1 or be indexed by the first index name associated with identifier-1. arithmetic- can be any arithmetic expression as described expression-1 and under the heading, "Arithmetic Expressions", in arithmetic-expression- Chapter 8 . However, any identifiers 2 appearing in any arithmetic expression in a SEARCH statement are subject to the same restrictions as identifier-3 and identifier-4. imperative-statement-1 each one or more imperative statements. and imperative-statement-2 Description If identifier-1 is a data item subordinate to a data item containing an OCCURS clause (that is, a two or three dimensional table), an index name must be associated with each dimension of the table represented by identifier-1. This is done through the INDEXED BY phrase of the OCCURS clause for identifier-1. Only the setting of the index name associated with identifier-1 (and the data item named by identifier-2 or index-name-1, if used) is modified by the execution of the SEARCH statement. To search an entire two or three dimensional table, you must execute a SEARCH statement several times. You must also use SET statements to adjust index names to appropriate settings. SEARCH Statement - Format 1 When you use a format 1 SEARCH statement, a serial search is performed, starting with the current index setting of the index name associated with identifier-1. If the index name associated with identifier-1 contains a value corresponding to an occurrence number greater than the highest possible occurrence number of identifier-1, the SEARCH statement is terminated immediately. If an AT END phrase is specified, the imperative-statement-1 within the phrase is executed. If an AT END phrase is not specified, control passes to the next executable statement following the SEARCH statement. If the index name associated with identifier-1 contains a value corresponding to an occurrence number within the range of identifier-1, the SEARCH statement evaluates each condition in the order that it is written. If none of the conditions is satisfied, the index name for identifier-1 is incremented to reference the next occurrence. The process of evaluating each condition is then repeated using the new index name settings, provided the value of the index name is within the permissible range of occurrences for identifier-1. If the new setting is not within range, the search terminates in the manner described in the preceding paragraph. If one of the conditions is satisfied when it is evaluated, the search terminates and the imperative-statement-1 associated with that condition is executed. The index name retains the value at which it was set when the condition was satisfied. After execution of imperative-statement-1, if it is not a GO TO statement, control passes to the next executable sentence. VARYING Phrase If you use the VARYING phrase, index-name-1 may or may not appear in the INDEXED BY phrase of identifier-1. If it does, index-name-1 is the index name used in the search. If it does not, or if you specified identifier-2 instead, the first (or only) index name given in the INDEXED BY phrase of identifier-1 is used for the search. If you specify VARYING index-name-1, and index-name-1 is associated with a different table (that is, does not appear in the INDEXED BY phrase of identifier-1), 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. If you specify VARYING identifier-2, and identifier-2 is an index data item, the data item represented by identifier-2 is incremented in the same way as for index-name-1 in the preceding paragraph. If identifier-2 is not an index data item, the data item referenced by identifier-2 is incremented by one (1) at the same time that the index referenced by the index name associated with identifier-1 is incremented. If you do not specify the varying phrase, the index name used for the search operation is the first (or only) index name appearing in the INDEXED BY phrase of identifier-1. Any other index name for identifier-1 remains unchanged. The flowchart in Figure 9-7 shows the execution of a format 1 SEARCH statement specifying two WHEN phrases.
[]
Figure 9-7. Execution of Format 1 SEARCH Statement SEARCH Statement - Format 2 If you use a format 2 SEARCH statement, a binary search is done. The index name used in the search is the first (or only) index name appearing in the INDEXED BY phrase of identifier-1. Any other index names for identifier-1 remain unchanged. The results of a format 2 SEARCH statement are predictable only under three conditions: 1. The data in the table is ordered in the same manner as described in the ASCENDING (or DESCENDING) KEY clause associated with the description of identifier-1. 2. The contents of the key or keys referenced in the WHEN clause are sufficient to identify a unique table element. [REV BEG] 3. All entries in the table contain valid data. In other words, the table is filled properly depending on how it is defined. You can use the OCCURS DEPENDING ON clause to control the size of the table and make sure all elements contain valid data. Alternatively, if the table uses the ASCENDING KEY clause, any unused alphanumeric entries in the table should be at the end of the table and filled with HIGH-VALUES. If the table uses the DESCENDING KEY clause, any unused alphanumeric entries in the table should be at the end of the table and filled with LOW-VALUES.[REV END] When a data name in the KEY clause of identifier-1 is referenced, or when a condition name associated with a data name in the KEY clause of identifier-1 is referenced, all preceding data names in the KEY clause of identifier-1 or their associated condition names must also be referenced. The maximum number of reference keys is 12. When the search begins, the initial setting of the index name associated with identifier-1 is ignored and its setting is varied during the search in such a way as to make the search as fast as possible. Of course, it is not set to a value outside of the possible range of occurrence numbers for the table. If there is no possible valid setting for the index name associated with identifier-1 that satisfies all the conditions specified in the WHEN clause, control passes to the imperative statement specified in the AT END phrase if it has been specified. If the AT END phrase is not specified, and the conditions cannot all be satisfied, control passes to the next executable sentence. In either case, the final setting of the index is not predictable. If all conditions in the WHEN phrase can be satisfied, the index indicates the occurrence that allows the conditions to be satisfied, and control passes to imperative-statement-2. After execution of imperative-statement-2, providing that imperative-statement-2 does not contain a GO TO statement, control passes to the next executable sentence. The example below uses both formats of the SEARCH statement. In the format 1 SEARCH statement, a search is performed to find out if a carton is part of the inventory. If it is part of the inventory, its first dimensions (there are five possible) as well as its part number are displayed. The second SEARCH statement is a format 2 SEARCH. It is used to find a container of a given volume whose height and width both equal 5. Example WORKING-STORAGE SECTION. 01 PARTS-TABLE. 02 PARTS-INFO OCCURS 10 TIMES INDEXED BY PT-INDX. 03 PART-NAME PIC X(20). 03 PART-NUMBER PIC X(10). 03 MEASURES OCCURS 5 TIMES ASCENDING KEY IS VOLUME, HEIGHT, WIDTH INDEXED BY IND-T2. 04 HEIGHT PIC 999V999. 04 WIDTH PIC 999V999. 04 LGTH PIC 999V999. 04 VOLUME PIC 999V999. 77 NEXT-NUM PIC 99 VALUE 1. 01 CONTAINER-INFO. 02 PT-NAME PIC X(20). 02 FILLER PIC X(5) VALUE SPACES. 02 PT-NO PIC X(10). 02 FILLER PIC X(3) VALUE SPACES. 02 DIMENSIONS. 04 H PIC 999V999. 04 W PIC 999V999. 04 L PIC 999V999. 04 V PIC 999V999. : PROCEDURE DIVISION. SEARCH-PARTS-INFO. SET IND-T2 PT-INDX TO 1. SEARCH PARTS-INFO AT END PERFORM UNFOUND-RTN WHEN PART-NAME (PT-INDX) = 'CARTON' PERFORM FOUND-IT. : FOUND-IT. MOVE MEASURES (PT-INDX, IND-T2) TO DIMENSIONS. MOVE PART-NAME (PT-INDX) TO PT-NAME MOVE PART-NUMBER (PT-INDX) TO PT-NO DISPLAY HEADER. DISPLAY CONTAINER-INFO. DISPLAY SPACES. : CONTAINER-SELECTION. DISPLAY "THIS SECTION SEARCHES FOR A CONTAINER". DISPLAY "OF A SPECIFIED VOLUME WHOSE HEIGHT AND". DISPLAY "WIDTH BOTH ARE FIVE. PLEASE SPECIFY THE VOLUME". DISPLAY "REQUIRED (IN AN EVEN NUMBER OF CUBIC FEET.)". ACCEPT NEEDED-VOLUME. SEARCH-PARTS-TABLE. MOVE 1 TO NEXT-NUM. SET PT-INDX IND-T2 TO 1. SEARCH ALL MEASURES AT END PERFORM NO-SUCH-CONTAINER. WHEN VOLUME (PT-INDX, IND-T2) = NEEDED-VOLUME AND WIDTH (PT-INDX, IND-T2) = 5 AND HEIGHT (PT-INDX, IND-T2) = 5 PERFORM FOUND-IT. : NO-SUCH-CONTAINER. IF NEXT-NUM < 10 ADD 1 TO NEXT-NUM SET PT-INDX TO NEXT-NUM SET IND-T2 TO 1 PERFORM SEARCH-PARTS-TABLE ELSE DISPLAY "NO CONTAINER MEETS THESE REQUIREMENTS".


MPE/iX 5.0 Documentation