2018 Nov 27 2:17 PM
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.
2018 Nov 28 9:43 AM
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 = ',' ).
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.
2018 Nov 27 3:01 PM
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.
2018 Nov 27 3:15 PM
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...
2018 Nov 27 3:28 PM
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.
2018 Nov 27 3:50 PM
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).
2018 Nov 27 3:59 PM
2018 Nov 28 2:39 AM
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 ) ).
2018 Nov 28 9:43 AM
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 = ',' ).
2018 Nov 28 12:50 PM
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 = ',' ).
2018 Nov 29 2:30 AM
| User | Count |
|---|---|
| 6 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |