Alex Rivera | Logout

Can I skip a whole file with the <> operator?

Asked 2009-09-14T15:03:28.227
10

The following Perl code has an obvious inefficiency;

while (<>)
{
if ($ARGV =~ /\d+\.\d+\.\d+/) {next;}
... or do something useful
}

The code will step through every line of the file we don't want.

On the size of files this particular script is running on this is unlikely to make a noticeable difference, but for the sake of learning; How can I junk the whole file <> is working and move to the next one?

The purpose of this is because the sever this script runs on stores old versions of apps with the version number in the file name, I'm only interested in the current version.

Edit
Report

1 Answer

20

Paul Roub's solution is best if you can filter @ARGV before you start reading any files.

If you have to skip a file after you've begun iterating it,

while (<>) {
    if (/# Skip the rest of this file/) {
        close ARGV;
        next;
    }
    print "$ARGV: $_";
}
answered 2009-09-14T16:15:30.493

Your Answer