http://www.perlmonks.org?node_id=337666


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.