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

REDUCE not concatenating strings

MarcC
Explorer
0 Likes
1,102

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.

3 REPLIES 3
Read only

MarcC
Explorer
0 Likes
1,089

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
Read only

0 Likes
1,014

You can achieve the same by sing backticks ` instead of single quotes '.

Read only

0 Likes
993

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.