2007 Mar 12 8:02 PM
Hi friends,
I am looping an internal table based on a where condition.
DATA : BEGIN OF itab1 OCCURS 0,
vbeln LIKE vbak-vbeln,
matnr LIKE mara-matnr,
END OF itab1.
data : v_tabix type c.
itab1-vbeln = '1'.
itab1-matnr = 'A'.
APPEND itab1.
itab1-vbeln = '2'.
itab1-matnr = 'B'.
APPEND itab1.
itab1-vbeln = '3'.
itab1-matnr = 'C'.
APPEND itab1.
itab1-vbeln = '4'.
itab1-matnr = 'A'.
APPEND itab1.
itab1-vbeln = '5'.
itab1-matnr = 'D'.
APPEND itab1.
itab1-vbeln = '6'.
itab1-matnr = 'E'.
APPEND itab1.
itab1-vbeln = '7'.
itab1-matnr = 'A'.
APPEND itab1.
itab1-vbeln = '8'.
itab1-matnr = 'F'.
APPEND itab1.
loop at itab1 where matnr = 'A'.
v_tabix = sy-tabix.
endloop.
After the end the value in v_tabix is 7, that the last hit where itab1-matnr = 'A'.
However I want to find out the second last value as well. In this case I need to find out, the last and secondlast scuuessful hits.
4 and 7 should be my results.
Any suggestions.
Madhu.
2007 Mar 12 8:10 PM
Hi,
loop at itab1 where matnr = 'A'.
at end of matnr
v_ltabix = sy-tabix.
move 'Y to v_flg.
endat.
if v_flg ne 'Y'.
v_stabix = sy-tabix.
endif.
endloop.
Here v_ltabix contains 7 and v_stabix cotnains 4
aRs
2007 Mar 12 8:08 PM
Define an internal table for your index and add every match.
data: g_t_tabix type table of SYTABIX initial size 0.
loop at itab1 where matnr = 'A'.
append sy-tabix to g_t_tabix.
endloop.
Guenther
2007 Mar 12 8:10 PM
Hi,
loop at itab1 where matnr = 'A'.
at end of matnr
v_ltabix = sy-tabix.
move 'Y to v_flg.
endat.
if v_flg ne 'Y'.
v_stabix = sy-tabix.
endif.
endloop.
Here v_ltabix contains 7 and v_stabix cotnains 4
aRs
2007 Mar 12 8:16 PM
Thanks for the suggestions. Will try out nd would get back.
Shreekant...
2007 Mar 12 8:25 PM
Thanks aRS for the suggestion. But In this the at end of matnr wont work because in the structure i have vbeln before matnr and it changes everytime.
I cant change the structure of itab1.
Thanks for the suggestion.
Madhu.
2007 Mar 12 8:37 PM
Did you try my suggestion with the internal table?
If you really need the last and second last records, just sort the internal table with the tabix values descending and then read the 1. and 2. entry; use thos to read the rows of your actual itab.
Guenther
2007 Mar 12 8:38 PM
2007 Mar 12 8:32 PM
Your sort should be VBELN MATNR.
My assumption is that for every VBELN you need to find
the last and second last MATNR.
aRs
2007 Mar 12 8:36 PM
NO i cant sort.
Anyways I modified the code a bit and now it works.
LOOP AT itab1 WHERE matnr = 'A'.
CLEAR v_flg.
AT END OF matnr.
AT LAST.
v_ltabix = sy-tabix.
MOVE 'Y' TO v_flg.
ENDAT.
IF v_flg NE 'Y'.
v_stabix = sy-tabix.
ENDIF.
ENDLOOP.
v_ltabix = last value.
v_stabix = second last value.
Thanks again.
Madhu
2007 Mar 12 8:34 PM
even i suggest the procedure same a madhu reddy suggested. because here u can get which ever position u want using index of the new table
2007 Mar 12 8:37 PM