in reply to Cleaner way of looping through a file and stripping only certain lines?
If it's not a huge file, you could read it into memory and use map to iterate over all the lines:
#!/usr/bin/perl -w use strict; my $ifile = shift || die "No filename provided\n"; open IFILE, '<', "$ifile" || die "Couldn't open file: $!"; open OFILE, '+>', 'new-data' || die "Couldn't open outfile: $!"; chomp(my @lines = <IFILE>); close IFILE; my @matches = map { /^@.*mail(\d+).*\z/? $1 <= 8? $1: ( ): $_ } @lines +; map { print OFILE "$_\n" } @matches; close OFILE;
But I don't see anything wrong with the way you did it, except that I would recommend error-checking for the case where a filename isn't passed to the program.
Update: Fixed to take skip saving anything in the case where the captured pattern isn't <= 8.
Update 2: Cleaned up syntax further.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Cleaner way of looping through a file and stripping only certain lines?
by texasperl (Sexton) on Dec 08, 2006 at 17:08 UTC |