Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Replace first blank line in a text file with headers.

by ExReg (Priest)
on Mar 24, 2016 at 15:34 UTC ( [id://1158729]=note: print w/replies, xml ) Need Help??


in reply to Replace first blank line in a text file with headers.

Assuming you make the changes suggested by neilwatson, another thing that might not go as expected is if you have blank lines further down in the file.

while( my $line = <$fh_in> ) {

will cause any blank line, not just the first one, to be replaced by "Name, City, State, Zip".

Replies are listed 'Best First'.
Re^2: Replace first blank line in a text file with headers.
by Laurent_R (Canon) on Mar 24, 2016 at 18:33 UTC
    Those changes won't work anyway as the code stands, because the OP code is opening the same file twice, once in read mode and once in write mode. That's a no-go.

    The Tie::File module is a solution, a one-liner using in-line option is another. Else, opening another file for writing, making the necessary changes, and renaming the files is another option.

    Then only should we worry about avoiding to change other lines, one way or another.

      I would have some code to make a backup of some kind, even if I used Tie::File. There is just so much that can go wrong when debugging a new version or some slightly different input file shows up and program fails. I've saved my own butt many times with a .bak1, .bak2 file...This stuff can go in a temp directory that periodically gets cleaned out. Creating long lived "junk" is also a problem, but of a different sort. If you have backups from yesterday that's a good thing, but I never want to resort to that to fix something that just went wrong 5 minutes ago.

      Too true.

        Should be pretty easy. Just open a new output file, print your header to it. Then cycle through your input file, skipping all blank lines. Then close your output file.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found