Binary Search in Standard Tables 

If you read entries from standard tables using a key other than the default key, you can use a binary search instead of the normal linear search. To do this, include the addition BINARY SEARCH in the corresponding READ statements.

READ TABLE <itab> WITH KEY = <f> <result> BINARY SEARCH.

and

READ TABLE <itab> WITH KEY <k1> = <f1> ... <kn> = <fn> <result>
                                                       BINARY SEARCH.

The standard table must be sorted in ascending order by the specified search key. The BINARY SEARCH addition means that you can access an entry in a standard table by its key as quickly as you would be able to in a sorted table.

Example

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE STANDARD TABLE OF LINE.

DO 4 TIMES.
  LINE-COL1 = SY-INDEX.
  LINE-COL2 = SY-INDEX ** 2.
  APPEND LINE TO ITAB.
ENDDO.

SORT ITAB BY COL2.

READ TABLE ITAB WITH KEY COL2 = 16 INTO LINE BINARY SEARCH.

WRITE: 'SY-SUBRC =', SY-SUBRC.

The output is:

SY-SUBRC =    0

The program fills a standard table with a list of square numbers and sorts them into ascending order by field COL2. The READ statement uses a binary search to look for and find the line in the table where COL2 has the value 16.