for my $file (@files) { open my $fh, '<', $file or die "$file: $!"; while (<$fh>) { /^[2-6] .+$/ or next; chomp; push @results, $_; } close $fh; }