-1

I have my internal table it_mseg. In this table, there is a field called amnt.

I want to check for each row in it_mseg, if the field amnt is greater equal 10. If it is, I want to delete it from the internal table.

So that at the end, when I display the table using ALV-Grid, only the rows where the value of the field amnt is lower equal 10 will be displayed.

I know that this is somehow done with Loop at it_mseg, but I just can't get it right.

EDIT: I want to do it with a loop, so I can do something more complex than just GE 10.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Kevin Mueller
  • 628
  • 3
  • 10
  • 23
  • 1
    [ABAP documentation: Processing Statements for Internal Tables](https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abentable_processing_statements.htm) – Sandra Rossi Jun 04 '18 at 14:22
  • Possible duplicate of [Delete the current row from an internal table in a loop](https://stackoverflow.com/questions/23632238/delete-the-current-row-from-an-internal-table-in-a-loop) – Gert Beukema Jun 04 '18 at 15:07

1 Answers1

5

You can do it with LOOP, but even simplier with DELETE:

DELETE it_mseg WHERE amnt GT 10.

If you still want to do it with LOOP (because you want to check/change something else in the internal table):

LOOP AT it_mseg
     ASSIGNING FIELD-SYMBOL(<ls_mseg>).
  DATA(lv_tabix) = sy-tabix. "save sy-tabix for later use
... "do somthing else
  IF <ls_mseg>-amnt GT 10.
    DELETE it_mseg INDEX lv_tabix.
  ENDIF.
... "do something else
ENDLOOP.
József Szikszai
  • 4,791
  • 3
  • 14
  • 24
  • Hmm yes, but how could I do it with loop? In case I want to do something a bit more complex than just GT 10. – Kevin Mueller Jun 04 '18 at 12:29
  • Then I suggest you to add that information in the question post, because Jozsef's answer hits the nail for the requirements you've put there. – VXLozano Jun 04 '18 at 12:39
  • 1
    @Lucky: I added that to my answer. If you want to have more complex condition, than "GT 10" than you have to modify it accordingly. – József Szikszai Jun 04 '18 at 12:41
  • 1
    `DELETE it_mseg` behaves the same as `DELETE it_mseg INDEX lv_tabix`, but now you can remove the lv_tabix definition and have less places for errors – András Jun 04 '18 at 13:21
  • DELETE it_mseg is the best approach rather than use a use a LOOP. I personally think that deleting elements from an internal table that I'm looping is not ok. – Nelson Miranda Jun 04 '18 at 14:38
  • @NelsonMiranda, why? It is about the same speed, and everything else is exactly the same. – András Jun 04 '18 at 14:59
  • @András Because when doing that, the indexes are regenerated affecting the performance. In my opinion, in this case, is better to use DELETE it_mseg. – Nelson Miranda Jun 04 '18 at 21:29