Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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.


Comment on Re: deleting lines from file
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-08-01 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls