Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Re: Skip a line

by lisaw (Beadle)
on Nov 11, 2002 at 19:02 UTC ( [id://212038]=note: print w/replies, xml ) Need Help??


in reply to Re: Skip a line
in thread Skip a line

Hello, When making the change from opening a file by > to >>, I am finding that when adding info to the file all data lines are being copied and rewritten. So I end up with multiple entries of same line being written every time an addition is made. Any way around this?

Replies are listed 'Best First'.
Re: Re: Re: Skip a line
by tachyon (Chancellor) on Nov 11, 2002 at 19:29 UTC

    >> appends or in other words ADDS TO THE END OF the existing file. All the data lines are not being copied, they are simply being left there and new data added to the end. Here is a guide for you:

    my $file = '/foo/file.txt'; open IN, $file or die "Can't read $file perl says $!\n"; my @lines = <IN>; close IN; for my $line (@lines) { # do stuff with the lines } # rewrite the file in its entirity open OUT, '>$file' or die "Can't write $file perl says $!\n"; print OUT $line[0]; # print out the first line from the old fil +e print OUT @other_stuff; # print whatever else you want close OUT;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found