2012 Dec 07 6:04 PM
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
2012 Dec 07 6:19 PM
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
2012 Dec 07 6:22 PM
thanks for promt reply,
can we use Binary Search, even though table doesn't contain numeric fields?
2012 Dec 07 6:24 PM
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.
2012 Dec 07 6:30 PM
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.
2012 Dec 07 6:29 PM
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
2012 Dec 07 6:33 PM
2015 Jan 14 10:17 AM
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.
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |