2007 Sep 28 4:46 PM
This is more of a gripe than a question, but Ive marked it as a question anyway in case someone can shed some light. It is prompted by a couple of recent forum questions.
Suppose you have a character string with spaces in it and want to replace all the spaces with dashes. The following program uses the REPLACE statement a couple of ways to try to do this:
REPORT ztest MESSAGE-ID 00.
DATA: f1(20) VALUE 'ABCDEF GHIJK LMNO'.
* OK
WHILE sy-subrc = 0.
REPLACE space WITH '-' INTO f1.
ENDWHILE.
* REPLACE_INFINITE_LOOP
REPLACE ALL OCCURRENCES OF space IN f1 WITH '-'.The first attempt using a WHILE/ENDWHILE works, but if you try to do this all at once in the second it dumps with REPLACE_INFINITE_LOOP because it thinks that SPACE or ( ) has zero length.
But if SPACE has zero length, how can it do the replacement correctly in the first case? Is this a bug or a "design feature" of ABAP?
I thought I would bump this to the top to see if there were any further ideas before closing it.
Rob
Message was edited by:
Rob Burbank
2007 Sep 28 5:57 PM
Rob,
Very interesting, Tried it out a couple of times before I could actually believe it.
Went to the exception class (This is a catchable exception) and it had this description:
CX_SY_REPLACE_INFINITE_LOOP
REPLACE ALL OCCURENCES OF ... with blank search string causes endless loop.
So I guess no matter what you do, you cannot leave the search string blank when you use all occurrences of addition .
Regards,
Srihari
2007 Sep 28 4:54 PM
Hi Rob,
The reason for this exception is:
he running program, "ZETA_TEST3", tried to execute the command "REPLACE ALL CCURRENCES OF ' '". However, the field "' '" has a length of 0, which would have caused the command to perform an endless loop. For this reason, the program was terminated.
Regards
Sudheer
2007 Sep 28 4:57 PM
I understand that Sudheer, but why do the two versions behave differently?
Rob
2007 Sep 28 5:06 PM
Very interesting. Tried in different ways..but always dumps.
Nice find though.
Sri
2007 Sep 28 5:09 PM
Yeah - it seems silly on one hand. On the other, it forces you into more complex logic to do what should be a very simple thing.
Rob
2007 Sep 28 5:09 PM
HI,
TRY THIS,
DATA FIELD(20).
MOVE 'ABCDEF GHIJK LMNO' TO FIELD.
REPLACE ' ' WITH ' - ' INTO field.
WRITE:/10 FIELD.
REGARDS
SIVA
2007 Sep 28 5:18 PM
Siva - yes, that does something different, but I'm really more interested in why ALL OCCURRENCES doesn't work in this case.
Rob
2007 Sep 28 5:57 PM
Rob,
Very interesting, Tried it out a couple of times before I could actually believe it.
Went to the exception class (This is a catchable exception) and it had this description:
CX_SY_REPLACE_INFINITE_LOOP
REPLACE ALL OCCURENCES OF ... with blank search string causes endless loop.
So I guess no matter what you do, you cannot leave the search string blank when you use all occurrences of addition .
Regards,
Srihari
2007 Sep 28 6:00 PM
> Went to the exception class (This is a catchable
> exception) and it had this description:
> CX_SY_REPLACE_INFINITE_LOOP
> REPLACE ALL OCCURENCES OF ... with blank search
> string causes endless loop.
It seems that SAP doesn't want to give us a better explanation -:(
Greetings,
Blag.
2007 Sep 28 6:10 PM
2007 Sep 28 6:16 PM
Good question...Here comes my silly explanation...
REPLACE --> Took first occurrence and replace it...
REPLACE ALL OCCURENCES --> Took all occurrences...But as they are all SPACE, there's actually no size...So they can't be handled...
Greetings,
Blag,
2007 Sep 28 6:19 PM
I think deep down in the kernel this is how code for the replace statement has been written:
If replace <b>one occurrence of </b>
Try to replace the char
else.
<b>*all occurences of </b>
if string length > 0.
then replace.
else.
throw exception.
endif.
2007 Sep 28 6:20 PM
OK, but it doesn't work that way without the ALL OCCURRENCES. That's the question.
Rob
2007 Sep 28 6:31 PM
2010 May 19 2:26 PM
Today I stumbled over the same problem. If you have at least Kernel 7.00 (I think) you may use
REPLACE ALL OCCURRENCES OF REGEX '\s{1}' IN result WITH '_'.as replacement for
REPLACE ALL OCCURRENCES OF space IN result WITH '_'.(Just in case someone like me uses google in the future and finds this long dead thread.)
2010 May 19 2:30 PM
Hi,
This will also solve the problem.
REPLACE ALL OCCURRENCES OF ` ` IN f1 WITH '-'.
2010 May 19 2:42 PM
2016 May 17 2:28 PM
2010 May 19 2:45 PM
Yes - I know there's all sorts of workarounds. I was complaining about how it worked.
Note the date of the OP.
Rpb
2010 May 19 2:50 PM
Hi Rob,
I was aware of this thread long before, and just wanted to add my opinion.
Keshav