Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: In place search and replace with a hash

by hkates (Novice)
on Dec 28, 2014 at 03:29 UTC ( [id://1111513]=note: print w/replies, xml ) Need Help??


in reply to Re: In place search and replace with a hash
in thread In place search and replace with a hash

Anomalous, you're right. As I wrote in my original post, I have never actually used s/foo/bar other than in a one liner. I was looking for a way to find and replace strings in a file without writing to a new output file and when the replacement strings are not the same length as the sub-strings that are replaced. Clearly I am not doing this the correct way, and if there is a correct way, I haven't been able to find/understand it yet. What I want is to open a file and if a line is:

'nc7-52:p39|read1|1|19|296|0.0642'

and change it to:

C_fraterna_nc7-52

using a hash where $hash{nc7-52}=C_fraterna_nc7-52

Update: From this example it looks like it would be easy to print to output, but some lines in the file will not be modified and I still want to preserve them.

sorry, this thread may be a bit out of control since I had too many problems to begin with

  • Comment on Re^2: In place search and replace with a hash

Replies are listed 'Best First'.
Re^3: In place search and replace with a hash
by AnomalousMonk (Archbishop) on Dec 28, 2014 at 03:52 UTC
    From this example it looks like it would be easy to print to output ...

    As a first step, create a small test input file and just print the altered and unaltered lines to standard output, e.g.,
        print $my_new_line_what_is_just_the_way_i_want_it;
    When this limited, test output looks ok, start to think about the process of opening a new file and writing to that file instead of STDOUT. When that looks ok, start thinking about all the input/output file verification and renaming that has to happen. (I'm sure there are modules to facilitate the "edit in place" process, but I don't know what they are off-hand.)


    Give a man a fish:   <%-(-(-(-<

Re^3: In place search and replace with a hash
by AnomalousMonk (Archbishop) on Dec 28, 2014 at 03:39 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-20 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found