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

Binary search a sorted internal table

Former Member
0 Likes
8,945

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,850

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

11 REPLIES 11
Read only

Former Member
0 Likes
2,850

read table itab with key field1 = v_field binary search.

REgards,

Ravi

Read only

0 Likes
2,850

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.

Read only

0 Likes
2,850

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

Read only

Former Member
0 Likes
2,850

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.

Read only

Former Member
0 Likes
2,850

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

Read only

rahulkavuri
Active Contributor
0 Likes
2,850

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

Read only

suresh_datti
Active Contributor
0 Likes
2,850

Yes you can.. press F1 on Binary & SAP help explains you the way it searches on a SORTED table.

Regards,

Suresh Datti

Read only

Former Member
0 Likes
2,850

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

Read only

Former Member
0 Likes
2,850

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>

Read only

0 Likes
2,850

after remove TABLE, it works. thanks for you all!

Read only

Former Member
0 Likes
2,851

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