Application Development 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: 

Please explain how to use binary search in this loop.

Former Member
0 Kudos
1,747

Hi,

I want to use the binary search in below Read please give me solution....

     
LOOP AT it_vbfa_temp ASSIGNING <fs_vbfa_temp>.

      CLEAR is_ekbz1.
      READ TABLE it_ekbz1 INTO is_ekbz1 WITH KEY xblnr = <fs_vbfa_temp>-deliv flag = space.

      IF  sy-subrc = 0.

        w_tabix = sy-tabix.

        is_ekbz1-tknum = <fs_vbfa_temp>-shipno.
        is_ekbz1-flag  = 'X'.

        MODIFY it_ekbz1 FROM is_ekbz1 INDEX w_tabix TRANSPORTING tknum flag.

 ENDLOOP.

12 REPLIES 12

venkat_o
Active Contributor
0 Kudos
341

Hi Srinivas, <li>If you want to use BINARY SEARCH with READ statement, First thing , Table must be sorted by Key fields which you mention in WITH KEY <key1> <key2>.


SORT it_ekbz1 by xblnr flag.
"Then use read with BINARY SEARCH.
Thanks Venkat.O

Former Member
0 Kudos
341

Dear Venkat,

I am modifying the key filed which is using in the search. If i used the binary search it is failing in lot of cases.

Reagrds,

Srinivas

0 Kudos
341

How have you defined the table IT_EKBZ1 ? And in the code snippet you have posted i do not see you modifying the key field.

BR,

Suhas

Former Member
0 Kudos
341

Dear Suhas,

I am modifying the fieild FLAG which is using in the Read Statement inside the loop. Thats why i am unable to sort it before the loop and unable to use Binary search.

Reagrds,

Srinivas.

0 Kudos
341

Sorry. My bad !!

I dont understand why your BINARY SEARCH fails ? Do you have multiple records for the same keys in you table IT_EKBZ1 ?

BR,

Suhas

Former Member
0 Kudos
341

Hi Srininas ,

Please do like below

SORT it_ekbz1 by xblnr.

LOOP AT it_vbfa_temp ASSIGNING <fs_vbfa_temp>.
 
      CLEAR is_ekbz1.
      READ TABLE it_ekbz1 INTO is_ekbz1 WITH KEY xblnr = <fs_vbfa_temp>-deliv flag = space BINARY SEARCH.
 
      IF  sy-subrc = 0.
 
        w_tabix = sy-tabix.
 
        is_ekbz1-tknum = <fs_vbfa_temp>-shipno.
        is_ekbz1-flag  = 'X'.
 
        MODIFY it_ekbz1 FROM is_ekbz1 INDEX w_tabix TRANSPORTING tknum flag.
 
 ENDLOOP.

Thanks,

Chinmay

Edited by: Chinmay Kumar Ghosh on Apr 6, 2010 9:08 AM

Former Member
0 Kudos
341

Hi Srininas,

To use binary search you need to first sort the table preferabely with the field you want to use as the key in the read statement.

Once you have done this you can use the addition Binary search in the read statement.

So in your case before the Loop statement use

Sort table it_ekbz1 by xblnr .

and the modified read statement will look like

READ TABLE it_ekbz1 INTO is_ekbz1 WITH KEY xblnr = <fs_vbfa_temp>-deliv flag = space binary search.

Regards,

Arun

Former Member
0 Kudos
341

First sort the internal table on the key fields used in the read statement, then add the keyword "BINARY SEARCH" at the end of Read Statement.

Regards

Vinod

0 Kudos
341

Dear All,

If we are modifying any field which is using in the binary search in side the loop the binary search will not work ..Thats why i am unable to use binary search woth sorting the key Fields.

But i have to use binary beacuse of the more number of records in both the tables.

Reagrds,

Srinivas.

Former Member
0 Kudos
341

Hi,

Thanks for the inputs given.. Please find the solution for the same ....

 

   CLEAR is_ekbz1.
    is_ekbz1-flag = 'X'.
    MODIFY it_ekbz1 FROM is_ekbz1 TRANSPORTING flag WHERE flag IS INITIAL.

    SORT it_ekbz1 BY ebeln xblnr lifnr flag.


  LOOP AT it_vbfa_temp ASSIGNING <fs_vbfa_temp>.
 
      CLEAR is_ekbz1.
      READ TABLE it_ekbz1 INTO is_ekbz1 WITH KEY xblnr = <fs_vbfa_temp>-deliv flag = 'X' BINARY SEARCH.
 
      IF  sy-subrc = 0.
 
        w_tabix = sy-tabix.
 
        is_ekbz1-tknum = <fs_vbfa_temp>-shipno.
        is_ekbz1-flag  = space.
 
        MODIFY it_ekbz1 FROM is_ekbz1 INDEX w_tabix TRANSPORTING tknum flag.
 
  ENDLOOP.

Regards,

Srinivas

Edited by: Srininas on Apr 6, 2010 12:16 PM

Edited by: Srininas on Apr 6, 2010 12:17 PM

0 Kudos
341

CLEAR is_ekbz1.

is_ekbz1-flag = 'X'.

MODIFY it_ekbz1 FROM is_ekbz1 TRANSPORTING flag WHERE flag IS INITIAL.

SORT it_ekbz1 BY ebeln xblnr lifnr flag.

LOOP AT it_vbfa_temp ASSIGNING <fs_vbfa_temp>.

CLEAR is_ekbz1.

READ TABLE it_ekbz1 INTO is_ekbz1 WITH KEY xblnr = <fs_vbfa_temp>-deliv flag = 'X' BINARY SEARCH.

IF sy-subrc = 0.

w_tabix = sy-tabix.

is_ekbz1-tknum = <fs_vbfa_temp>-shipno.

is_ekbz1-flag = space.

MODIFY it_ekbz1 FROM is_ekbz1 INDEX w_tabix TRANSPORTING tknum flag.

SORT it_ekbz1 BY ebeln xblnr lifnr flag.

ENDLOOP.

0 Kudos
341

Sry In my las reply sorting wrong

Correct one is SORT it_ekbz1 By xblnr flag.

Regards,

Srinivas