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) {}

This says to globally replace every % with %25. If that replacement is
"true" (ie, if there were any % to replace), the while loop conditional
is true, so the block is executed (which does nothing, since it's
empty), and the conditional is checked again. Now it starts at the
beginning of the string and tries to replace every % with %25. If that
replacement is "true" . . . . etc etc etc

> I suspect that inserting a beginning % in the replacement string should
> be the problem (as successive application of the command will match
> it).

You are correct.

> What is the right way to do it?

Why are you using a while loop at all? What made you think that was
something that should be there?

If you want to replace globally, but only once, just do that:
$input =~ s/%/%25/g;

(There was also no reason for the /s modifier in your original)

> In the same idea, if I want to transform any single ' into a double '',
> I will have the same problem using :
>
> while ($input =~ s/'/''/sg) {}

No you won't. Here, you're changing all ' to " the first time through.
After that, the s/// can't find any ' in the string, so the while will
become false. It still makes more sense, however, to just do it once:
$input =~ s/'/"/g;

Paul Lalli

.



Relevant Pages

  • Sol8 and EVA hangs
    ... Secure Path 3.0D is loaded for channel failover. ... retrying command ... # Adapter hard loop ID address to use on the fibre channel bus. ...
    (SunManagers)
  • Summary: Sol8 and EVA hangs
    ... Oct 8 11:00:41 proddb SCSI transport failed: reason 'aborted': ... retrying command ... # Adapter hard loop ID address to use on the fibre channel bus. ... # before reporting I/O errors. ...
    (SunManagers)
  • Re: Using foreach loop to create radiobutton menu
    ... Your foreach loop is not the problem, the problem is how you define the -command. ... The only possible thing the interpreter can do is substitute the current value of range, which is likely the last value once your loop exited. ... One choice is double quotes. ... The double quotes means that $range gets expanded while in the loop, long before the puts command actually runs. ...
    (comp.lang.tcl)
  • Re: Issue implementing Runtime.exec() with StreamGobbler
    ... So jloader basically calls LoaderAutomationRun.java (starts the loop) ... As you can see the last line, the New Command Executer never returned. ... If your test driver is hanging, I'd guess that the test process itself is ...
    (comp.lang.java.programmer)
  • Re: What does Ctrl+C really do
    ... loop) to fgetsthe user's commands. ... As posted before there is a signal handler taking care of that. ... I interprete "windows terminal" as a Command Shell in MS-WIndows, ... Shell detects that the telnet client has been gone it terminates itself. ...
    (comp.os.vxworks)