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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Without seeing your "somefile.txt" I'm not sure what you are doing to get your $word. Certainly, the second chomp is superfluous as you can't remove the line terminator twice. My code below is the equivalent of yours except that I don't do the final chop as I can't tell whether it was necessary; it's easy enough to add back in. I perform the parsing of "somefile.txt" in a BEGIN {...} block before going on to append to the lines in the "address" file.

moritz points out that it can be a bad idea to edit a file in place. Perl does, however, have a handy -i flag for in-place editing which, if given an extension, will preserve your original file with that extension, the modified file taking the original name. Given a spurious "tcp" file and an "address" file

$ cat spw638403.txt aajkaj XYZtcpHJHKtcpKJHKH ashhgdasjh JKHKJHtcpUUHtcpHDKJHtcpJHDJAHD kjkjn NHYtcpHHGJJH $ cat spw638403.addr Address1= Address2= Address3= $

running this code (note the -i.bak flag

#!/usr/bin/perl -i.bak # use strict; use warnings; my @list = (); BEGIN { my $inFile = q{spw638403.txt}; open my $inFH, q{<}, $inFile or die qq{open: $inFile: $!\n}; while ( <$inFH> ) { chomp; push @list, ( split m{tcp}, ( split )[1] )[0]; } close $inFH or die qq{close: $inFile: $!\n}; } while ( <> ) { s{$}{$list[$. - 1]}; print; }

with the address file as the argument results in the original file preserved as spw638403.addr.bak and the modified file as spw638403.addr

$ ./spw638403 spw638403.addr $ cat spw638403.addr Address1=XYZ Address2=JKHKJH Address3=NHY $

I hope this is of use.

Cheers,

JohnGG

Update: Clarified point that the scripts needs the name of the file to be modified as an argument.

Update 2: Corrected senior moment with flag. It is, of course, the -i flag, not -p


In reply to Re: Need a little help appending lines by johngg
in thread Need a little help appending lines by perl4ever

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-06-25 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.