Example: binary search
The following example shows how you can code a binary search of a table.
Suppose you define a table that contains 90 elements
of 40 bytes each, and three 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:
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).
You can search this table by using the following statements:
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
If an entry is found in which each of the three keys
is equal to the value to which it is compared (VALUE-1
, VALUE-2
,
and VALUE-3
, respectively), PART-1
of
that entry is moved to OUTPUT-AREA
. If no matching
key is found in the entries in TABLE-A
, the NOENTRY
routine
is performed.