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

Concatenating strings within REDUCE

BTP_Architect
Participant
0 Likes
12,579

Hello,

I'm trying to use new syntax provided by 7.4 and later versions

My requirement is to concatenate the fields of one internal table into a string.

DATA(lv_text) = REDUCE string( INIT lv_where_clause TYPE string
                           FOR ls_werks  IN lt_werks
                           NEXT lv_where_clause = lv_where_clause && ls_werks-low && ',' ).

This will replace the loop on the internal table with concatenation at each step.

My problem with reduce is how to get rid of the last comma?

Many Thanks.

Moez.

1 ACCEPTED SOLUTION
Read only

matt
Active Contributor
8,607

When I needed to do this, I used the example in the documentation. https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abenreduce_text_abexa.htm

The simplest thing is to use an extra variable. Excuse the renaming of your variable, but I detest prefexing to indicate variable type.

DATA(text) = REDUCE string( INIT where_clause = '' sep = ''
                           FOR werk_range_line IN werk_selection
                           NEXT where_clause = where_clause && sep && werk_range-low
                                sep = ',' ).
9 REPLIES 9
Read only

Jean_Sagi
Participant
0 Likes
8,607

What about:

NEXT lv_where_clause =  ',' && lv_where_clause && ls_werks-low 

That way you'll end with a first comma instead of a last comma. So get rid of the first character instead of the last one.

Last but not least the lv_where_clause will be a string of the form 1000,2000,3000... (Assuming your logistic centers are 1000,2000,3000, etc...) this seems to me not a proper way to a where clause. Shouldn't it be something like "1000","2000","3000",... ?

J.

Read only

0 Likes
8,607

Thanks Jesus for your feedback

Your proposal will give something like: ,,,100020003000 (all comma in the beginning)

They should be separated by comma.

For the lv_where_clause, yes I need it without double quotation marks for further processing...

Read only

Jean_Sagi
Participant
0 Likes
8,607

Like Home says : Ouch !!
You know the copy paste syndrome... 😉

By now you have already guessed it, but what about:

NEXT lv_where_clause = lv_where_clause && ',' && ls_werks-low 

J.

Read only

0 Likes
8,607

Yes you are right, by this way I will have the comma as first character and remove it using the shift

SHIFT lv_text BY 1 PLACES LEFT.

In my first coding it was also possible to remove the last comma using SHIFT.

But my question was more about the REDUCE and if there is a way to manage such concatenation within REDUCE (without adding any other instruction like SHIFT).

Read only

0 Likes
8,607

Yep. That's though !.

Read only

DoanManhQuynh
Active Contributor
8,607

i suggest using condition to check if its last record, like this:

DATA(lv_text) = REDUCE string( INIT lv_where_clause TYPE string
FOR i = 1 UNTIL i = lines( lt_werks )
NEXT lv_where_clause = COND #( WHEN i < lines( lt_werks ) THEN lv_where_clause && lt_werks[ i ]-low && ','
ELSE lv_where_clause && lt_werks[ i ]-low ) ).
Read only

matt
Active Contributor
8,608

When I needed to do this, I used the example in the documentation. https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abenreduce_text_abexa.htm

The simplest thing is to use an extra variable. Excuse the renaming of your variable, but I detest prefexing to indicate variable type.

DATA(text) = REDUCE string( INIT where_clause = '' sep = ''
                           FOR werk_range_line IN werk_selection
                           NEXT where_clause = where_clause && sep && werk_range-low
                                sep = ',' ).
Read only

0 Likes
8,607

Thanks Matthew

This is what I was searching for

I just changed the initialization of the where_clause (I exchanged where_clause = ' ' by where_clause type string)

Otherwise the text will be on one character...

Final solution:

DATA(text) = REDUCE string( INIT where_clause TYPE string sep = ''
                           FOR werk_range_line IN werk_selection
                           NEXT where_clause = where_clause && sep && werk_range-low
                                sep = ',' ).
Read only

matt
Active Contributor
0 Likes
8,607

Try using | as in the SAP example.