Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Binary search option with Read statement

Former Member
0 Kudos

Hi,

There are any chances that the Read statement with Binary search option to fail, even though the key exists??

Here the itab is sorted in descending order. and then some duplicates are removed using the delete adjacent statement.

Regards,

Sandhip.

12 REPLIES 12

Former Member
0 Kudos

Hi,

No it cannot fail unless there is no matching record.

Check ones again the record exist in both the tables( loop table and Read table).

revrt back if any issues

regards

Naveen

0 Kudos

Hi Naveen,

One more clarification.. the order of sorting matters or not??

Regards,

Sandhip

0 Kudos

Hi SANDY ,

The order of sorting does not matter .

Please check out how the Binary search technique works , so that you can understand it better.

Regards

Arun

0 Kudos

Hi Sandy,

When u Binary Search with a Key make sure you have first sorted your internal table with the same Key.

Regards,

Sonal

0 Kudos

No matter of sorting for Binary Search. If U sort ascending and ur value is in first half, the search moves to the top half of the records and if u sort in descending order, search moves to the last half and proceeds further.

awrd points if helpful

Bhupal

Former Member
0 Kudos

Hi,

And make sure the table is sorted by the same key which is used in the binary Search..

Regards,

Sonal

Former Member
0 Kudos

Hi sandy

There are no chances that a read statement fails because of binary search

The problem may come with With Key ... binary search is used to increase the searching process

Before using Binary search first sort your internal table

Donot forgot to reward points if helpful

Hitesh

Former Member
0 Kudos

Hi,

Check the following program:

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 = 20 INTO LINE BINARY SEARCH.

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

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 20. The sy-subrc is 8 because its fail.

Regards,

Bhaskar

Former Member
0 Kudos

Hi Sandy,

Table should be sorted in Ascending order

Also make sure Key for sort and binary search should be same.

Regards,

Mohaiyuddin

Former Member
0 Kudos

sort table by the same key with which the table is read.

regards,

kushagra

aris_hidalgo
Contributor
0 Kudos

Hi,

Here is an example:

sort itab1 by a b c.

loop at itab2 into wa_itab2.

read table itab1 into wa_itab1

with key a = wa_itab2-a

b = wa_itab2-b

c = wa_itab2-c

binary search.

if sy-subrc = 0.

wa_output-a = wa_itab1-a.

endif.

endloop.

Another alternative is to use sorted tables.

Hope it helps...

P.S. Please award points if it helps...

Former Member
0 Kudos

Hi Sanddy,

In binary search the table entreis are divided into two equal parts and it will search for data in only one half depending on comparision of the middle value with value you entered.so for this searching type table must be sorted.

for any doubt plz reply.

With Regards.