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

Inconsistent Behavior of the REPLACE statement

Former Member
0 Likes
3,982

This is more of a gripe than a question, but I’ve 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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,245

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

19 REPLIES 19
Read only

Former Member
0 Likes
3,245

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

Read only

0 Likes
3,245

I understand that Sudheer, but why do the two versions behave differently?

Rob

Read only

Former Member
0 Likes
3,245

Very interesting. Tried in different ways..but always dumps.

Nice find though.

Sri

Read only

0 Likes
3,245

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

Read only

Former Member
0 Likes
3,245

HI,

TRY THIS,

DATA FIELD(20).

MOVE 'ABCDEF GHIJK LMNO' TO FIELD.

REPLACE ' ' WITH ' - ' INTO field.

WRITE:/10 FIELD.

REGARDS

SIVA

Read only

0 Likes
3,245

Siva - yes, that does something different, but I'm really more interested in why ALL OCCURRENCES doesn't work in this case.

Rob

Read only

Former Member
0 Likes
3,246

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

Read only

0 Likes
3,245

> 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.

Read only

0 Likes
3,245

Srihari and Blag - but why the difference?

Rob

Read only

0 Likes
3,245

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,

Read only

0 Likes
3,245

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.

Read only

0 Likes
3,245

OK, but it doesn't work that way without the ALL OCCURRENCES. That's the question.

Rob

Read only

0 Likes
3,245

Srihari - it looks to me like you're saying it's a bug.

Rob

Read only

0 Likes
3,245

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.)

Read only

0 Likes
3,245

Hi,

This will also solve the problem.


REPLACE ALL OCCURRENCES OF ` ` IN f1 WITH '-'.

Read only

0 Likes
3,245

You can use...

TRANSLATE f1 USING ' -'.

Read only

0 Likes
3,245

Worked Well

Read only

Former Member
0 Likes
3,245

Yes - I know there's all sorts of workarounds. I was complaining about how it worked.

Note the date of the OP.

Rpb

Read only

0 Likes
3,245

Hi Rob,

I was aware of this thread long before, and just wanted to add my opinion.

Keshav