Application Development and Automation 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: 
Read only

Regarding Binary Search to Read Table

arun_purohit
Participant
0 Likes
4,073

Dear ABAP Experts,

I have a question regarding Binary Search for reading an internal table.

Following are my points of query-

1) As per my understanding, we can use Binary Search only if parameter used with "WITH KEY" clause is a numeric field and internal table is sorted. Please correct me if I am wrong.

2) I have tried using Binary Search with sorted tables, but some times it has given unexpected incorrect results. Not sure what went wrong.

3) Is it possible to read an internal table with 8-9 fields (most of them are non-numeric) with "WITH KEY" clause using Binary Search.

Thanks,

Arun Purohit

Moderator message: covered by ABAP documentation -> question status removed

Message was edited by: Thomas Zloch

Dear ABAP Experts,

I have a question regarding Binary Search for reading an internal table.

Following are my points of query-

1) As per my understanding, we can use Binary Search only if parameter used with "WITH KEY" clause is a numeric field and internal table is sorted. Please correct me if I am wrong.

2) I have tried using Binary Search with sorted tables, but some times it has given unexpected incorrect results. Not sure what went wrong.

3) Is it possible to read an internal table with 8-9 fields (most of them are non-numeric) with "WITH KEY" clause using Binary Search.

Thanks,

Arun Purohit

Moderator message: covered by ABAP documentation -> question status removed

Message was edited by: Thomas Zloch

7 REPLIES 7
Read only

Former Member
0 Likes
1,983

First and most important condition you need to sort the internal table before using the read table..binaary search..

Use the reaad key field in the correct formate.. check the sy-subrc aafter reading the internal table.

You can use 8-9 field in the reaad table

Read only

0 Likes
1,983

thanks for promt reply,

can we use Binary Search, even though table doesn't contain numeric fields?

Read only

Former Member
0 Likes
1,983

Hi,

First of all to use binary search the internal table should be sorted as you already know. There are other things you should know.

1> Before using binary search you need to sort the table by the keys that you use while searching using read table.

2>The order of keys used for sort must be same as the keys used to read the table.

Eg: suppose ITAB has 4 fields. Field1, field2, field3 and field4.

and you want to read the table using Field1 and field2 in this order.

READ TABLE itab INTO wa WITH KEY field1 = val1 field2 = val2.

then you should sort your table before reading like this.

SORT itab by field1 field2.

Hope its clear.

thanks.

Read only

0 Likes
1,983

Hi Aswatha,

but is it mandatory to use numeric fields as search criteria, to use Binary Search? or any type of field (char, numc & int) can be the criteria.

Read only

amy_king
Active Contributor
0 Likes
1,983

Hi Arun,

The keys don't need to be numeric, but the table does need to be sorted in ascending order according to the specified search key(s). There is some documentation on binary search here.

Cheers,
Amy

Read only

0 Likes
1,983

Thanks Amy.

Read only

Former Member
0 Likes
1,983

Hi Amy ,

I have simple question for u i.e,

if we sort the internal table by two fields like

SORT ITAB BY FIELD1 FIELD2.

then if we use only one key field in the read statement for binary search what will happen

and what is the diff in between them,

ex -  READ TABLE ITAB INTO WA WITH KEY FIELD2 - .......... BINARY SEARCH.

what will happen ,

plz answer my question this is an interview question.