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: 

Sy-TABIX into a LOOP AT and READ TABLE

zhlish
Explorer
0 Kudos
2,215

Hi guys, there is someone can explain me what is the value of sy-tabix when it's into a LOOP AT stantment where I also have a READ table?
I declared a variable to maintain the index of my itab for deleting the row with condition, but why it doesn't work well if i just directly write : DELETE itab INDEX sy-tabix?

DATA: lv_tabix TYPE sy-tabix.

LOOP AT itab.

lv_tabix = sy-tabix.

READ TABLE itab2 WITH KEY werks = itab-werks BINARY SEARCH.
IF itab-daycont < itab2-switch.
DELETE itab INDEX lv_tabix. "istead of sy-tabix
ENDIF.
ENDLOOP.

1 ACCEPTED SOLUTION

abo
Active Contributor
1,855

READ TABLE will change sy-tabix, that's why you need to save it before.

8 REPLIES 8

abo
Active Contributor
1,856

READ TABLE will change sy-tabix, that's why you need to save it before.

touzik_itc
Active Participant
1,855

To delete the current row in a LOOP block you can use DELETE statement without INDEX.

DELETE itab.

Sandra_Rossi
Active Contributor
0 Kudos
1,855

Another option by indicating explicitly that you delete the current line in a loop:

DELETE itab USING KEY loop_key.

(the advantage compared to "DELETE itab" is that the syntax checker tells you when it's outside a loop; when you use "DELETE itab" outside a loop, the syntax checker doesn't warn, but that will produce a runtime error).

matt
Active Contributor
0 Kudos
1,855

Why not just modify your program a little bit and figure it out yourself? Oh, and LOOP AT itab? Do not use tables with header lines. Very bad practice.

Also, READ ... BINARY SEARCH? Just define itab2 as a sorted table.

Whereever you go this sample code from it is over twenty years out of date.

DATA lv_tabix TYPE sy-tabix.
LOOP AT itab INTO DATA(wa).
  WRITE / sy-tabix, 'Just after loop'.
  lv_tabix = sy-tabix.
  READ TABLE itab2 WITH KEY werks = itab-werks BINARY SEARCH.
  WRITE / sy-tabix, 'Just after read'.
IF itab-daycont < itab2-switch.
DELETE itab INDEX lv_tabix. "istead of sy-tabix ENDIF. ENDLOOP.

zhlish
Explorer
0 Kudos
1,855

Matthew, I appreciate your comment, but for someone just starting out with programming in general, is not that immediate to come to the tips you have given, and because I didn't find a way or an answer that actually answered my doubt I just simply used this platform as everyone do.

And lastly, this over twenty years out of date code, is my code.

matt
Active Contributor
1,855

You are using techniques that as a newbie you shouldn't be using. Tables with header lines (OCCURS) are a definite no-no. It could get you into very bad habits. ISure it's your code, but it looks like you're using a template from old training material to get you started. I strongly suggest getting some up-to-date training materials. As you progress also look at relevant parts of how to write Clean ABAP. It will give you many tips on how to write well. Remember, your program must not just work, it must also be maintainable by someone else, and be easy to change without breaking.

As you're a newbie, I did provide you with a way of figuring it out for yourself. Hence the WRITE statements.

I strongly suggest also that you learn how to debug. This can resolve many doubts you might have. For instance debugging your original program would have shown you, step by step, how the variables changed for each statement.

zhlish
Explorer
1,855

Thank you so much for all your suggestions

matt
Active Contributor
0 Kudos
1,855

Always glad to help someone willing to learn. 🙂