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: 

Using REDUCE to Sum a Table

ron_mitton
Explorer
0 Kudos

How can I convert this to a REDUCE expression ?

I can't convert the WHERE to WHILE

Thanks

DATA header_price TYPE bapicond-cond_value.
LOOP AT order_conditions_in ASSIGNING FIELD-SYMBOL(<cond>) WHERE cond_type = 'PR00'.
  header_price = header_price + <cond>-cond_type.
ENDLOOP.

"Unsuccessful attempt
"Message - The statement before "WHILE ..." was not closed (period missing).
DATA(header_price2) = REDUCE bapicond-cond_value( INIT p = 0 
                                                  FOR prices IN order_conditions_in 
                                                  while prices=>cond_type = 'PR00' 
                                                  NEXT p = p + prices-cond_value ).
1 ACCEPTED SOLUTION

MichaelSauter
Advisor
Advisor

Hey everyone,

I wrote a small program which does this aggregation in four ways (loop, reduce with for...in...where, while, reduce with for...while). Which one you like most is up to you, I'd go with option 2.

Kind regards,

Michael

types:
  begin of s_input,
    cond_type  type string,
    cond_value type i,
  end of s_input,
  t_input type standard table of s_input with empty key.


data(lt_input) = value t_input(
  ( cond_type = 'PR00' cond_value = 10 )
  ( cond_type = 'PR01' cond_value = 10 )
  ( cond_type = 'PR00' cond_value = 10 )
  ( cond_type = 'PR01' cond_value = 10 ) ).


" loop 
data lv_header_price1 type i.
loop at lt_input assigning field-symbol(<cond>)
  where cond_type = 'PR00'.

  lv_header_price1 = lv_header_price1 + <cond>-cond_value.
endloop.
write: / lv_header_price1.

" reduce with FOR ... IN ...WHERE
data(lv_header_price2) = reduce i( init p = 0
                                   for prices in lt_input
                                   where ( cond_type = 'PR00' )
                                   next p = p + prices-cond_value ).
write: / lv_header_price2.

" while
data lv_header_price3 type i.
data(lv_index) = 1.
while lv_index < lines( lt_input ) + 1.
  if lt_input[ lv_index ]-cond_type = 'PR00'.
    lv_header_price3 = lv_header_price3 + lt_input[ lv_index ]-cond_value.
  endif.
  lv_index = lv_index + 1.
endwhile.
write: / lv_header_price3.

" reduce with FOR ... WHILE
data(lv_header_price4) = reduce i( init p = 0
                                   for i = 1 while i < lines( lt_input ) + 1
                                   next p = cond #( when lt_input[ i ]-cond_type = 'PR00' then p + lt_input[ i ]-cond_value
                                                    else p ) ).
write: / lv_header_price4.
6 REPLIES 6

Sandra_Rossi
Active Contributor

You have to use WHERE, not WHILE. Why do you want to use WHILE?

ron_mitton
Explorer
0 Kudos

The documentation only mentions UNTIL and WHILE

REDUCE|NEW|VALUE type( ... FOR ... UNTIL|WHILE ...|... IN ... ... ) .

abo
Active Contributor

ron_mitton
Explorer
0 Kudos

Thank you.

This works:

DATA(header_price) = REDUCE bapicond-cond_value( INIT p = 0 
                      FOR prices IN order_conditions_in 
                      WHERE ( cond_type = 'PR00' ) 
                      NEXT p = p + prices-cond_value ).

horst_keller
Product and Topic Expert
Product and Topic Expert

Please be aware, that FOR has two variants:

FOR ... UNTIL|WHILE ... 
      | ... IN ... 

FOR ... UNTIL|WHILE has the semantics of statements DO|WHILE.

FOR ... IN has the semantics of LOOP.

You must not mix up the syntax.

MichaelSauter
Advisor
Advisor

Hey everyone,

I wrote a small program which does this aggregation in four ways (loop, reduce with for...in...where, while, reduce with for...while). Which one you like most is up to you, I'd go with option 2.

Kind regards,

Michael

types:
  begin of s_input,
    cond_type  type string,
    cond_value type i,
  end of s_input,
  t_input type standard table of s_input with empty key.


data(lt_input) = value t_input(
  ( cond_type = 'PR00' cond_value = 10 )
  ( cond_type = 'PR01' cond_value = 10 )
  ( cond_type = 'PR00' cond_value = 10 )
  ( cond_type = 'PR01' cond_value = 10 ) ).


" loop 
data lv_header_price1 type i.
loop at lt_input assigning field-symbol(<cond>)
  where cond_type = 'PR00'.

  lv_header_price1 = lv_header_price1 + <cond>-cond_value.
endloop.
write: / lv_header_price1.

" reduce with FOR ... IN ...WHERE
data(lv_header_price2) = reduce i( init p = 0
                                   for prices in lt_input
                                   where ( cond_type = 'PR00' )
                                   next p = p + prices-cond_value ).
write: / lv_header_price2.

" while
data lv_header_price3 type i.
data(lv_index) = 1.
while lv_index < lines( lt_input ) + 1.
  if lt_input[ lv_index ]-cond_type = 'PR00'.
    lv_header_price3 = lv_header_price3 + lt_input[ lv_index ]-cond_value.
  endif.
  lv_index = lv_index + 1.
endwhile.
write: / lv_header_price3.

" reduce with FOR ... WHILE
data(lv_header_price4) = reduce i( init p = 0
                                   for i = 1 while i < lines( lt_input ) + 1
                                   next p = cond #( when lt_input[ i ]-cond_type = 'PR00' then p + lt_input[ i ]-cond_value
                                                    else p ) ).
write: / lv_header_price4.