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: 

Index Value

Former Member
0 Kudos
139

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.

1 ACCEPTED SOLUTION

former_member194669
Active Contributor
0 Kudos
114

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

10 REPLIES 10

Former Member
0 Kudos
114

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

former_member194669
Active Contributor
0 Kudos
115

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

0 Kudos
114

Thanks for the suggestions. Will try out nd would get back.

Shreekant...

0 Kudos
114

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.

0 Kudos
114

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

0 Kudos
114

Your method works good.

Thanks Guenther and thanks aRs.

Madhu.

former_member194669
Active Contributor
0 Kudos
114

Your sort should be VBELN MATNR.

My assumption is that for every VBELN you need to find

the last and second last MATNR.

aRs

0 Kudos
114

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

Former Member
0 Kudos
114

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

former_member194669
Active Contributor
0 Kudos
114

Venkat,

I don't understand what you are suggesting?

aRs