‎2009 Feb 18 5:00 PM
Would the following binary search produce correct results?
Internal table IT is sorted by field1 field2 field3
read table IT with key field2 = 'X' field3 = 'Y'
I am not sure this is correct since the read statement makes no
reference to field1.
‎2009 Feb 18 5:11 PM
Binary search always use divide and conquer method:
So ex:
Field1 Field2 Field3
ZX BC ZA
XY DE YH
ZA AA ZA
After Sort on Field1 Field2 Field3
result will be:
XY DE YH
ZA AA ZA
ZX BC ZA
If we dont use Field on in read the result will not be accurate.
IF you want to use field2 .
USE :
Sort itab by field2 field3.
Read table itab with key field2 field3 BINARY SEARCH.
Hope this resolves your query.
Regards,
Gurpreet
‎2009 Feb 18 5:03 PM
use all the fields you used to sort , to read as well
this will:
read table IT with key field1 = 'X' field2 = 'X' field3 = 'Y'
‎2009 Feb 18 5:11 PM
Binary search always use divide and conquer method:
So ex:
Field1 Field2 Field3
ZX BC ZA
XY DE YH
ZA AA ZA
After Sort on Field1 Field2 Field3
result will be:
XY DE YH
ZA AA ZA
ZX BC ZA
If we dont use Field on in read the result will not be accurate.
IF you want to use field2 .
USE :
Sort itab by field2 field3.
Read table itab with key field2 field3 BINARY SEARCH.
Hope this resolves your query.
Regards,
Gurpreet
‎2009 Feb 18 5:12 PM
It depends on what you mean by "correct". Why not try it and see if you get the results you want?
Rob
‎2009 Feb 18 5:26 PM
You could determine the result by running this. You'll see that it returns the first row. It is not, by the way, a binary search. And there is no way to make it binary unless you provide the entire key.
TYPES: BEGIN OF _ty_1,
field1(1) TYPE c,
field2(1) TYPE c,
field3(1) TYPE c.
TYPES: END OF _ty_1.
DATA:
g_t_tab TYPE TABLE OF _ty_1,
g_s_line TYPE _ty_1.
g_s_line-field1 = 'A'.
g_s_line-field2 = 'X'.
g_s_line-field3 = 'Y'.
APPEND g_s_line TO g_t_tab.
g_s_line-field1 = 'B'.
g_s_line-field2 = 'X'.
g_s_line-field3 = 'Y'.
APPEND g_s_line TO g_t_tab.
g_s_line-field1 = 'C'.
g_s_line-field2 = 'X'.
g_s_line-field3 = 'Y'.
APPEND g_s_line TO g_t_tab.
SORT g_t_tab BY field1 field2 field3.
READ TABLE g_t_tab WITH KEY field2 = 'X' field3 = 'Y' into g_s_line.
WRITE:/ g_s_line-field1,sy-subrc.
‎2009 Feb 18 7:00 PM
Hi,
there is thumb rule when using a BINARY SEARCH.
when ever you are using a Binary Search for a read SORT the table before read with what ever fields you want, and use the same fields in sequence what you used for sorting ,
if you use different keys Binary search Fails but it works sometimes not always...
like
sort IT by field1 field2 field3
read table IT with key field1 = 'X' field2 = 'X' field3 = 'Y'
if you are using only field2 and field3 as key then sort internal table by field2 and field3 only.
Regards,
vinayaka