‎2006 Jun 07 4:53 PM
Hi,
Can I do a binary search for a sorted internal table? I used to use binary search in a standard table after sort it. But I really don't know how I can use the binary search for the defined sorted internal table. For example:
DATA IT_TBL LIKE SORTED TABLE OF SER03 WITH NON-UNIQUE KEY ZEILE MBLNR WITH HEADER LINE.
Thanks!
Ryan
‎2006 Jun 07 5:05 PM
Hi,
Since it is sorted table u cant sort the table again by any other key.
sorted table are alawys in sorted order .
u can use binary search for sorted table with the key define for sorting.
u cant use any other key to sort the table again.
Regards
Mark helpful answers
‎2006 Jun 07 4:54 PM
read table itab with key field1 = v_field binary search.
REgards,
Ravi
‎2006 Jun 07 4:59 PM
Yes, Ravi that is the way I try to use. It works for the standard table after I sort it. But it doesn't work for the defined sorted table. When I use
read table it_tbl with table key mblnr = m zeile = z binary search.
The error is:
The addition "binary search" cannot be used with index access or general table search.
‎2006 Jun 07 5:01 PM
remove table
read table it_tbl with <b>table</b> key mblnr = m zeile = z binary search.and use it.
read table it_tbl with key mblnr = m zeile = z binary search.Regards
vijay
‎2006 Jun 07 4:54 PM
hi ryan,
the prerequisite for a binary search on an internal table is it should be sorted.
READ TABLE itab WITH KEY k1 = v1 ... kn = vn
[BINARY SEARCH] [additions].
Effect
The system evaluates the specified key to identify the correct line. If the type of a value is not compatible with the type of the corresponding key field, the system uses MOVE logic to convert the value into the type of the component before reading the table. This is an asymmetric comparison logic, in which the component type takes precedence over the value type.
The way in which the system looks for an entry in the table depends on its table type. The system optimizeds the key access whenever possible
SORTED TABLE:
If the specified key fields form a left-justified extract of the table key or are identical with the entire table key, the search is binary, otherwise sequential.
HOPE THIS HELPS,
priya.
‎2006 Jun 07 4:55 PM
hi Ryan,
Binary Search is generally used for Read Statement statement I am not sure that you can use the same for Internal tables too i.e, the way in which you have used...
i.e, <b>READ TABLE ITAB WITH KEY MATNR = IT_MATNR binary search.</b>
Regards,
Santosh
‎2006 Jun 07 4:56 PM
hi u should binary search for a sorted internal tables, that makes sense otherwise the performance is the same as it was previously... generally used with the read statements...
LOOP AT T_VBRK_VBRP.
V_SYTAB = SY-TABIX.
AT NEW VBELN.
READ TABLE T_VBRK_VBRP INDEX V_SYTAB.
<b>READ TABLE IT_VBRK WITH KEY VBELN = T_VBRK_VBRP-VBELN BINARY SEARCH.</b>
IF SY-SUBRC = 0.
T_VBRK_VBRP-FKDAT = IT_VBRK-FKDAT.
MODIFY T_VBRK_VBRP TRANSPORTING FKDAT
WHERE VBELN = IT_VBRK-VBELN.
ENDIF.
ENDAT.
ENDLOOP.Please award points if found helpful
‎2006 Jun 07 4:56 PM
Yes you can.. press F1 on Binary & SAP help explains you the way it searches on a SORTED table.
Regards,
Suresh Datti
‎2006 Jun 07 4:59 PM
Hi,
you need to use ZEILE MBLNR keys while using binary serach.
read table itab with key zeile = '000'
MBLNR = v_mblnr binary search.regards
vijay
‎2006 Jun 07 4:59 PM
Hi,
Yes you can do that but you will be able to use only the keys which are already define for the sorted table.
If you use any other key than the already defined once you will get error.
You acn check the define keys by double clicking the type and going to key tab.
<b>Reward points if it helps.</b>
‎2006 Jun 07 5:06 PM
‎2006 Jun 07 5:05 PM
Hi,
Since it is sorted table u cant sort the table again by any other key.
sorted table are alawys in sorted order .
u can use binary search for sorted table with the key define for sorting.
u cant use any other key to sort the table again.
Regards
Mark helpful answers