2025 Mar 11 8:16 PM - edited 2025 Mar 11 8:16 PM
Why does the following code with REDUCE not concatenate the table into the string 'ABC'?
DATA t TYPE TABLE OF kna1.
t = VALUE #(
( kunnr = 'A' )
( kunnr = 'B' )
( kunnr = 'C' )
).
DATA(log1) = REDUCE string( INIT x1 = '' FOR wa IN t NEXT x1 = x1 && wa-kunnr ).
WRITE: /, log1. " A
DATA log2 TYPE string.
LOOP AT t INTO DATA(wa2).
log2 = log2 && wa2-kunnr.
ENDLOOP.
WRITE: /, log2. " ABC
The LOOP approach works just fine.
Why does the following code with REDUCE not concatenate the table into the string 'ABC'?
DATA t TYPE TABLE OF kna1.
t = VALUE #(
( kunnr = 'A' )
( kunnr = 'B' )
( kunnr = 'C' )
).
DATA(log1) = REDUCE string( INIT x1 = '' FOR wa IN t NEXT x1 = x1 && wa-kunnr ).
WRITE: /, log1. " A
DATA log2 TYPE string.
LOOP AT t INTO DATA(wa2).
log2 = log2 && wa2-kunnr.
ENDLOOP.
WRITE: /, log2. " ABC
The LOOP approach works just fine.
2025 Mar 11 8:24 PM
OK, I discovered it is because x1 was implicitly char(1) and could contain only one character. Here is the corrected code:
DATA t TYPE TABLE OF kna1.
t = VALUE #(
( kunnr = 'A' )
( kunnr = 'B' )
( kunnr = 'C' )
).
DATA(log1) = REDUCE string( INIT x1 = CONV STRING( '' ) FOR wa IN t NEXT x1 = x1 && wa-kunnr ).
WRITE: /, log1. " ABC
2025 Mar 12 8:35 AM
You can achieve the same by sing backticks ` instead of single quotes '.
2025 Mar 12 8:15 PM
Hi,
You can declare the type of the X1 variable inside the REDUCE statement. That's what I would do. More details can be found in the REDUCE documentation
Regards.
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |