amarceluk has asked for the wisdom of the Perl Monks concerning the following question:
Consider this:
# Open both IN_FILE and OUT_FILE here! while(<IN_FILE>) { chomp; next unless length; print OUT_FILE "$_\n"; }
I always come back to the following:This is obviously good advice and I want to take advantage of it. However, I'm not quite sure what the code should look like, specifically for finding and replacing all occurences of a string. Is it something like this? (Please forgive my probably-incorrect code.)
Do not slurp in a file into an array. Someday that input file will be huge. And that will happen when used for production data. And you're on vacation. And you'll have to come in to the office during that sunny day on the beach.
Read the input file line by line and act upon each line (here by potentially writing it to the output file.)
Will something like this work? If not, what will?open (INFILE, "input.txt"); open (OUTFILE, ">output.txt"); while (INFILE) { $_ =~ s/foo/bar/gms; print OUTFILE "$_\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: reading/writing line by line
by davorg (Chancellor) on May 22, 2002 at 15:12 UTC | |
|
Re: reading/writing line by line
by Joost (Canon) on May 22, 2002 at 15:23 UTC | |
|
Re: reading/writing line by line
by Biker (Priest) on May 22, 2002 at 15:14 UTC | |
by amarceluk (Beadle) on May 22, 2002 at 15:44 UTC | |
|
Re: reading/writing line by line
by Juerd (Abbot) on May 22, 2002 at 17:33 UTC | |
by amarceluk (Beadle) on May 22, 2002 at 18:22 UTC | |
by amarceluk (Beadle) on May 22, 2002 at 19:42 UTC | |
by Juerd (Abbot) on May 22, 2002 at 19:44 UTC | |
by amarceluk (Beadle) on May 22, 2002 at 19:51 UTC |