Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Escaping confusion in RE...

by freddo411 (Chaplain)
on Jul 05, 2007 at 22:15 UTC ( [id://625139]=perlquestion: print w/replies, xml ) Need Help??

freddo411 has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm trying to do a "simple" find and replace.
I'm looking for L:\Data\5555\
to replace with D:\Sites\5555\Data\

I also want to generalize the numeric part. So I tried this RE:
s#l:\\Data\\([0-9]*)\\#D:\\Sites\\$1\Data\\#i;
This has strange effects causing an infinite loop during my replace. The backslash character is causing me trouble.

I am not using these paths, just replacing them as text in a file.

Thanks in advance for your advice.

-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday

Replies are listed 'Best First'.
Re: Escaping confusion in RE...
by Joost (Canon) on Jul 05, 2007 at 22:21 UTC
Re: Escaping confusion in RE...
by runrig (Abbot) on Jul 05, 2007 at 22:25 UTC
    What makes you think there is an infinite loop? BTW, you only have one backslash before 'Data' on the RHS in your example. Other than that, you seem to have it correct, by just doubling up on all the backslashes.
      I didn't give enough context. My bad. The substitution does seem to work. Here's more context, and the error.
      while ( @file ) { s#l:\\Data\\([0-9]*)\\#D:\\Sites\\$1\\Data\\#i; print; }
      @file is filled with many lines like so: my @file = <FILE>;

      Here are the warnings:
      Use of uninitialized value in print at foo.pl line 44. Use of uninitialized value in substitution (s///) at foo.pl line 43. Use of uninitialized value in substitution (s///) at foo.pl line 43.

      -------------------------------------
      Nothing is too wonderful to be true
      -- Michael Faraday

        If @file is an array then you want a for loop, not a while loop. If you want to read lines from a file into $_ then you want while ( <FILE> ) {

        I.e., you do have an infinite loop, @file has a constant number of things in it, so it is always true, unless you shift or pop things from it in the loop. (but a for loop is probably more appropriate).

        ([0-9]*) will match zero or more digits.

        To be a little cleaner, you probably should match 1 or more digits using ([0-9]+)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://625139]
Approved by pKai
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-16 09:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found