Skip to Content
0
Former Member
Dec 28, 2012 at 07:39 AM

Binary search problem

680 Views

Dear Experts,

I am having trouble in reading internal table through binary search.

This is my code :

Case Ist:

SORT it_mseg by mblnr mjahr.
SORT it_mkpf by mblnr mjahr.



LOOP at it_mseg INTO wa_mseg.
id1 = sy-tabix.
READ TABLE it_mkpf INTO wa_mkpf with KEY mblnr = wa_mseg-mblnr mjahr = wa_mseg-mjahr BINARY SEARCH.
if sy-subrc = 0.
wa_mseg-budat = wa_mkpf-budat.
wa_mseg-bldat = wa_mkpf-bldat.
MODIFY it_mseg FROM wa_mseg INDEX id1.
ENDIF.
ENDLOOP.

In above case binary search is working fine.

Case 2nd :

sort it_mseg by matnr DESCENDING.
it_mseg2[] = it_mseg[].
SORT it_mseg2 STABLE by matnr DESCENDING budat DESCENDING.

LOOP at it_final INTO wa_final.
CLEAR rpu.
rpu = wa_final-rpu.
READ TABLE it_mseg INTO wa_mseg with KEY matnr = wa_final-matnr BINARY SEARCH. " In this case only the material with index 1 is read in all other cases sy-subrc become 4.

i am not getting why it is doing this as in 1st case it is executing properly.

if sy-subrc = 0.
LOOP at it_mseg2 INTO wa_mseg2 FROM sy-tabix.
if wa_final-MBWBEST gt 0.
MOVE-CORRESPONDING wa_final to wa_final3.
if wa_final-MBWBEST ge wa_mseg2-menge.
wa_final3-mblnr = wa_mseg2-mblnr.
wa_final3-budat = wa_mseg2-budat.
wa_final3-MBWBEST = wa_mseg2-menge.

wa_final-MBWBEST = wa_final-MBWBEST - wa_final3-MBWBEST.

IF wa_final3-budat IS NOT INITIAL.
wa_final3-cnt = s_date - wa_final3-budat. "LETZTABG.
ENDIF.

wa_final3-rpu = rpu * wa_final3-MBWBEST.
APPEND wa_final3 to it_final3.


ELSe. "wa_final-MBWBEST lt wa_mseg2-menge_i.
wa_final3-mblnr = wa_mseg2-mblnr.
wa_final3-budat = wa_mseg2-budat.
wa_final3-MBWBEST = wa_final-MBWBEST.

IF wa_final3-budat IS NOT INITIAL.
wa_final3-cnt = s_date - wa_final3-budat. "LETZTABG.
ENDIF.
wa_final3-rpu = rpu * wa_final3-MBWBEST.
APPEND wa_final3 to it_final3.
wa_final-MBWBEST = 0.
ENdif.

ENDIF.
at END OF matnr.
exit.
ENDAT.
ENDLOOP.
ENDIF.
ENDLOOP.

Please help

Thanks,

Amar