Application Development and Automation 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: 
Read only

Sum internal table column without loop

former_member2492
Active Participant
27,960

is there a better way to do this sum:

 LOOP AT lt_mbew INTO DATA(ls_mbew).
lv_sum += ls_mbew-stprs.
ENDLOOP.

Thank you,

1 ACCEPTED SOLUTION
Read only

ThorstenHoefer
Active Contributor
23,617

Hi Jonathan,

you should check the reduce function:

This is explained in this blog: https://blogs.sap.com/2017/05/25/replace-the-loop-for-reduce-operator/

      <fs_ekpo>-netwr = REDUCE netwr( INIT val TYPE netwr
                                      FOR wa IN
                                      FILTER #( it_komv
                                                USING KEY key_kposn
                                                WHERE kposn EQ CONV #( <fs_ekpo>-ebelp ) )
                                      NEXT val = val + wa-kwert ).

Greetings

3 REPLIES 3
Read only

ThorstenHoefer
Active Contributor
23,618

Hi Jonathan,

you should check the reduce function:

This is explained in this blog: https://blogs.sap.com/2017/05/25/replace-the-loop-for-reduce-operator/

      <fs_ekpo>-netwr = REDUCE netwr( INIT val TYPE netwr
                                      FOR wa IN
                                      FILTER #( it_komv
                                                USING KEY key_kposn
                                                WHERE kposn EQ CONV #( <fs_ekpo>-ebelp ) )
                                      NEXT val = val + wa-kwert ).

Greetings

Read only

0 Likes
23,617

Hi Thorsten,I checked it but how would you write it in this case?

Read only

ThorstenHoefer
Active Contributor
23,617

Hi Joinathan,

In your case, it should be:

lv_sum = REDUCE #( INIT val = 0
                        FOR wa IN  lt_mbew NEXT val = val + wa-stprs ).

or with yourtype like num10, or i, ....

lv_sum = REDUCE yourtype( INIT val = 0
                        FOR wa IN  lt_mbew NEXT val = val + wa-stprs ).