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: 

Internal tables issue

ashok_2329
Explorer
0 Kudos
536

I have an internal table with 300 records. How can i delete every 3rd record of my internal table

1 ACCEPTED SOLUTION

shantraj
Explorer
0 Kudos
408

Check the below.

Data: lv_index type sy-tabix.
loop at it_data assigning field symbol(<lfs_data>).
lv_index = sy-tabix.
  check lv_index mod 3 = 0.
   clear <lfs_data>-fieldname.
endloop.
delete it_data where fieldname is initial.<br>
11 REPLIES 11

Sandra_Rossi
Active Contributor
408

You can look at all possible operations on internal tables here: https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abentable_processing_...

Concerning the logic of deleting lines number 3, number 6, number 9, etc., I guess you won't have any difficulty knowing the statements to process the internal tables.

ThorstenHoefer
Active Contributor
0 Kudos
408

Hi,

have you tried this

loop at it_data into data(wa_data).
check sy-tabix mod 3 = 0.
delete it_data.
endloop.

0 Kudos
408

Hi,

I was trayed this but it will delete every record after 1 and 2.

I am getting only first two records in my output after use this statement.

matt
Active Contributor
0 Kudos
408

The problem is that after the deletion sy-tabix will always be three.

0 Kudos
408

Can you add your own variable like

i_cnt

loop at it_data into data(wa_data).
  add 1 to i_cnt.
  check i_cnt mod 3 = 0.
  delete it_data.
endloop.

lenapadeken
Product and Topic Expert
Product and Topic Expert
408

Hi ashok_2329,

Since ABAP Release 7.87/7.55, SAP BTP ABAP Environment 2202, there's the addition STEP, which you can use for defining a step size. For example:

DELETE itab STEP 3.

Link to documentation (https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapdelete_itab_lines...) and blog post (https://blogs.sap.com/2022/07/04/reverse-loop-finally-possible-with-step-addition-for-abap-internal-...).

Best regards,

Lena

ThorstenHoefer
Active Contributor
408

I haven't tested this. But this could work:

DATA(output) =
      value #(
        FOR  i = 1 THEN i + 3  WHILE i <= lines( itab )
        ( itab[ i ] )  ).


output =
      value #(
        base output
        FOR  i = 2 THEN i + 3  WHILE i <= lines( itab )
        ( itab[ i ] )  ).

shantraj
Explorer
0 Kudos
409

Check the below.

Data: lv_index type sy-tabix.
loop at it_data assigning field symbol(<lfs_data>).
lv_index = sy-tabix.
  check lv_index mod 3 = 0.
   clear <lfs_data>-fieldname.
endloop.
delete it_data where fieldname is initial.<br>

0 Kudos
408

Thanks a lot it's working

matt
Active Contributor
408

Homework question is it?

matt
Active Contributor