abapinternal-tables

Count itab rows that meet some condition?


I get a internal table from a Function Module call that returns ~ 100 rows. About 40% of the rows are not relevant to me because I only need the entries with PAR1 = "XYZ". On SQL tables (transparent tables), I can use a

select count(*) from tab where PAR1 = "XYZ" 

to get the number of valid entries.

Looking at the documentation, all I could find was the READ Table syntax to iterate through the table. My current approach is to basically have a loop and increase if the row contains the value I want. But this seems very inefficient.

Is there a better approach for my requirement?


Solution

  • Do whatever feels right to you. With ~100 rows, virtually nothing will make a huge difference in runtime. For me, stability would be more important than speed in this case.

    That being said, you could try this:

    LOOP AT lt_my_table TRANSPORTING NO FIELDS WHERE par1 = 'XYZ'.
      ADD 1 TO l_my_counter.
    ENDLOOP.