COBOL


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.

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).

can be searched using the following instructions:

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

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.

*********************************************************

SEARCH:
01 ARRAY-VALUES.
   05 FILLER PIC 999V99 VALUE  11.11.
   05 FILLER PIC 999V99 VALUE  22.22.
   05 FILLER PIC 999V99 VALUE  33.33.
   05 FILLER PIC 999V99 VALUE  44.44.
   05 FILLER PIC 999V99 VALUE  55.55.
   05 FILLER PIC 999V99 VALUE  66.66.
   05 FILLER PIC 999V99 VALUE  77.77.
   05 FILLER PIC 999V99 VALUE  88.88.
   05 FILLER PIC 999V99 VALUE  99.99.
   05 FILLER PIC 999V99 VALUE 111.11.

01 ARRAY-TBL REDEFINES ARRAY-VALUES.
   05 ARRAY-ELEMENT OCCURS 10 TIMES
      ASCENDING KEY IS AE-KEY
      INDEXED BY AE-INDEX.
      10 AE-KEY PIC 999V99. . . .

PROCEDURE DIVISION.
.
.
.

SET AE-INDEX TO 1
MOVE 2 TO RETURN-CODE
SEARCH ARRAY-ELEMENT
    AT END
        MOVE 4 TO RETURN-CODE
    WHEN AE-KEY(AE-INDEX) = 77.77
        CONTINUE
    WHEN AE-KEY(AE-INDEX) = 88.88

        MOVE 0 TO RETURN-CODE
END-SEARCH

Values after execution:
Return-code = 2
AE-INDEX points to the TABLE-ELEMENT that equals 77.77

*********************************************************

DELAY in Program:
01  DELAY-AMT      PIC S9(08)  COMP  VALUE 0.
01  WS-DATEX.
     05  WS-CCYYMMDD    PIC  9(08).
01  WS-TIMEX.
     05  WS-HHMMSSHH  PIC 9(08).

PROCEDURE DIVISION.
050-MAIN-LOGIC.
   ACCEPT WS-DATEX FROM DATE  YYYYMMDD
   ACCEPT WS-TIMEX FROM TIME
   DISPLAY 'CURRENT DATE - ' WS-DATEX
   DISPLAY 'CURRENT TIME - ' WS-TIMEX

   DISPLAY '----------------'
   MOVE 10 TO DELAY-AMT
   DISPLAY 'DELAY AMT - ' DELAY-AMT
   CALL 'ILBOWAT0'  USING DELAY-AMT
   ACCEPT WS-TIMEX  FROM TIME
   DISPLAY 'CURRENT TIME - ' WS-TIMEX
   DISPLAY '--------------'
   MOVE 30 TO DELAY-AMT
   DISPLAY 'DELAY AMT - ' DELAY-AMT
   CALL 'ILBOWAT0' USING DELAY-AMT
   ACCEPT WS-TIMEX FROM TIME
   DISPLAY 'CURRENT TIME - ' WS-TIMEX
   DISPLAY '---------------------------'
  GOBACK.