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: 

Dummy LET expression in REDUCE

srikanthnalluri
Active Participant
0 Kudos
TYPES: BEGIN OF ty_ekpo,
         werks TYPE werks_d,
         menge TYPE bstmg,
       END OF ty_ekpo,
       tt_ekpo TYPE TABLE OF ty_ekpo WITH DEFAULT KEY.

DATA: lt_werks TYPE tt_ekpo.

DATA(lt_ekpo) = VALUE tt_ekpo(
                        ( werks = 1000 menge = CONV bstmg( '100.50' ) )
                        ( werks = 1000 menge = CONV bstmg( 100 ) )
                        ( werks = 1001 menge = CONV bstmg( '100.60' ) )
                        ( werks = 1001 menge = CONV bstmg( 100 ) )
                        ( werks = 1002 menge = CONV bstmg( '100.70' ) )
                        ( werks = 1002 menge = CONV bstmg( 100 ) ) ).

BREAK-POINT.
LOOP AT lt_ekpo ASSIGNING FIELD-SYMBOL(<fs>)
                GROUP BY ( werks = <fs>-werks )
                ASCENDING
                ASSIGNING FIELD-SYMBOL(<grp>).

**  Two Statements
*    DATA(lt_werks1) = VALUE tt_ekpo( FOR m IN GROUP <grp> ( m ) ).
*    lw_werks = REDUCE #( INIT r_wa TYPE ty_ekpo
*                         FOR wa IN lt_werks1
*                         NEXT r_wa-menge = r_wa-menge + wa-menge
*                              r_wa-werks = wa-werks ).
*    APPEND lw_werks TO lt_werks.

* Reduce
  DATA(lw_total) = REDUCE ty_ekpo(
                     INIT r_total TYPE ty_ekpo
                     FOR wa IN VALUE tt_ekpo( FOR m IN GROUP <grp> ( m ) )
                     LET x = wa IN
                     NEXT r_total-menge = wa-menge + r_total-menge
                     r_total-werks = wa-werks ).
  APPEND lw_total TO lt_werks.
ENDLOOP.

cl_demo_output=>display( lt_werks ).

In the above example, i am trying to add Quantities for each plant. It is working fine with the REDUCE Operator how ever i'm forced to use LET expression. (There is no use with variable 'x' )

Why i added LET: When i debug the REDUCE code, after "FOR m IN GROUP <grp>" statement, the cursor is going to the "NEXT r_total-menge" statement, just to prevent that i have added LET statement.

Can't we achieve this with out LET. Please correct me.

Thanks

1 ACCEPTED SOLUTION

DoanManhQuynh
Active Contributor
0 Kudos

First I dont understand your reason to add LET statement because with no LET it still sum data correctly and have same output as you do with two statements part.

Second, I think you dont have to using FOR 2 times, it could be simple like this:

 DATA(LW_TOTAL) = REDUCE TY_EKPO(
INIT R_TOTAL TYPE TY_EKPO
FOR M IN GROUP <GRP>
NEXT R_TOTAL-MENGE = M-MENGE + R_TOTAL-MENGE
R_TOTAL-WERKS = M-WERKS ).
APPEND LW_TOTAL TO LT_WERKS.

2 REPLIES 2

DoanManhQuynh
Active Contributor
0 Kudos

First I dont understand your reason to add LET statement because with no LET it still sum data correctly and have same output as you do with two statements part.

Second, I think you dont have to using FOR 2 times, it could be simple like this:

 DATA(LW_TOTAL) = REDUCE TY_EKPO(
INIT R_TOTAL TYPE TY_EKPO
FOR M IN GROUP <GRP>
NEXT R_TOTAL-MENGE = M-MENGE + R_TOTAL-MENGE
R_TOTAL-WERKS = M-WERKS ).
APPEND LW_TOTAL TO LT_WERKS.

0 Kudos

Thank you