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

Re: How can I find and delete a line from a file?

by dimar (Curate)
on Nov 13, 2004 at 03:50 UTC ( [id://407586]=note: print w/replies, xml ) Need Help??


in reply to How can I find and delete a line from a file?

Another alternative is to use Tie::File. This lets you treat a file as a regular array variable. This is very convenient because it is easy to understand what is going on, as long as you are familiar with perl arrays.

use Tie::File; my( $file_name, $arg1, $arg2 ) = @ARGV; # open the file with tie tie my @file_lines, 'Tie::File', $file_name or die; # delete the first line shift @file_lines; # filter out lines containing 'sandwich' @file_lines = grep !/^$arg1,$arg2,/, @file_lines; # close the file with untie. # IMPORTANT: always untie when you are done! untie @file_lines or die "$!";

Update: Note that perl does not actually load the file into the array. It only appears to do so.

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

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

    No recent polls found