03-15-2022 4:51 PM
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 ).
03-17-2022 2:43 PM
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.
03-15-2022 5:28 PM
03-15-2022 6:07 PM
The documentation only mentions UNTIL and WHILE
REDUCE|NEW|VALUE type( ... FOR ... UNTIL|WHILE ...|... IN ... ... ) .
03-15-2022 6:28 PM
03-15-2022 6:55 PM
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 ).
03-16-2022 9:16 AM
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.
03-17-2022 2:43 PM
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.