Re: Help, infinite loop on a simple replacement??



pierre_catello@xxxxxxxx wrote:
> Hello,
>
> When trying to replace every % by %25 in a file, my substitution
> command goes into an infinite loop.
>
> while ($input =~ s/%/%25/sg) {}
I guess you mixed up s///g with m//g. $input =~ s/%/%25/sg will always
return the number of substitutions made(s/// returns 0/1 if there is no
'g' modifier). So as long as you get any success match, the condition
in "while" clause will be true. so you get an infinite while loop..

to replace globally, just use the following line:

$input =~ s/%/%25/sg;

Xicheng
>
> I suspect that inserting a beginning % in the replacement string should
> be the problem (as successive application of the command will match
> it).
> What is the right way to do it?

.



Relevant Pages