Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Inherited a perl script...need help (s///)

by tye (Sage)
on Nov 11, 2015 at 20:25 UTC ( [id://1147494]=note: print w/replies, xml ) Need Help??


in reply to Inherited a perl script...need help

Because:

$line =~ s/0030/9999/g; $line =~ s/0031/9999/g; $line =~ s/0884/9999/g; $line =~ s/0716/9999/g;

- tye        

Replies are listed 'Best First'.
Re^2: Inherited a perl script...need help (s///)
by iceotron (Initiate) on Nov 11, 2015 at 20:31 UTC
    Thank you! That segment of code was replacing 0030, 0031 ect with 9999. If I remove that coding, how do I accomplish the same result?
      If I remove that coding, how do I accomplish the same result?

      If you remove something, and want the same result back, you can just put the code back in. Same result as before is practically guaranteed.

      I could guess that your real problem is a bit different: you removed lines that should have done X, but replaced 0030 with 9999 instead. You want to know, what code should be put in the place of those lines, so it does what you want - X.

      This would be my guess of your problem, which brings us to a problem of mine: I want to help you, yet I don't know what X stands for.

      - Luke

        I tried the code Stevie gave me, but it did the same thing my original code was doing. It found any match of 4 digits and replaced it with 9999. This led me to a way of patching the issue and fixing it in a text editor using block selection. Replacing 0030 with AAAA, replacing 0031 with BBBB ect ect. Basically I need the code to match the 4 character number and replace with a 4 character number. Any numbers of more or less character in a field should be ignored. I hope this helps clarify. My code that I used to clunk my way through the issue.

        $line =~ s/0030/AAAA/g; $line =~ s/0031/BBBB/g; $line =~ s/0884/CCCC/g; $line =~ s/0716/DDDD/g; $line =~ s/0528/EEEE/g;

      As I said in the CB, if you only want to replace exact matches (eg: 0300), make sure there isn't a number before or after it:

      s/(?!<\d)0300(?!=\d)/9999/g
        s/(?!<\d)0300(?!=\d)/9999/g

        should be

        s/(?<![0-9])0300(?![0-9])/9999/g

        My main point is that both look-behind and look-ahead were using the wrong syntax. My replacement of \d with [0-9] is likely not required.

        - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found