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

Re: Problem with String replace in file

by meraxes (Friar)
on Oct 22, 2007 at 17:23 UTC ( #646503=note: print w/ replies, xml ) Need Help??


in reply to Problem with String replace in file

The | is the "or" operator in a regular expression. You need to escape it for your regex to work:

s/\|\|/|\\N|/g;
--
meraxes


Comment on Re: Problem with String replace in file
Select or Download Code
Re^2: Problem with String replace in file
by philosophia (Sexton) on Oct 22, 2007 at 17:31 UTC
    if I use

    s/\|\|/|\\N|/g;

    it replaces || with ||. The \N seems to be an invisible newline character, and I want it to just be '\N'.

      I suspect your problem is that you are writing back to the filehandle IN when you (presumably) only opened it for read.

      open( IN, '<', $filename ); # input open( OUT, '>', 'newfilename.txt' ); # output while ( <IN> ) { s/\|\|/|\\N|/g; print OUT; } close IN; close OUT;
      --
      meraxes
        This was correct, thank you. || is replaced by |\N| throughtout newfilename.txt.

      \n will result in a newline.
      \N will result in a syntax error.
      \\N will give \N.

      If you're not getting \N from \\N, there's something you haven't showed us.

      Update: meraxes caught the problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://646503]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-12-22 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (115 votes), past polls