Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: deleting lines from file

by AcidHawk (Vicar)
on Mar 18, 2004 at 12:30 UTC ( #337666=note: print w/replies, xml ) Need Help??

in reply to deleting lines from file

Just a few comments (I know it's a little late but hopefully adds more clarity).

Firstly, once you read in a line from oldfile you need to keep it somewhere to check it against $valueToDelete. There are many ways to do this. These include putting each line into an array or even a hash etc.

Secondly, does newfile have anything in it..? if you are appending to newfile your open should look like

open my $ff, ">>newfile" or die "open failed: $!\n";
or if you are creating newfile from scratch, your open should look like
open my $ff, ">newfile" or die "open failed: $!\n";
Also if you are creating the file there will be nothing in it to read so you don't need to do the while loop or the chomp. You can simply do the print if the value you want to print is not like the $valueToDelete. Should you choose to put the lines from oldfile into an array your code would look something like
foreach my $line (@oldlines) { unless ($line eq $valueToDelete) { print $ff "$line\n"; } }
Another quick note is that when you are printing to newfile you must use your filehandle $ff.
Of all the things I've lost in my life, its my mind I miss the most.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://337666]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2017-04-27 21:55 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (514 votes). Check out past polls.