for my $x (0..$#all) { if($line =~ /$all[$x]/) { print F2 $line; } } simply: foreach (@all) { print F2 $line if $line =~ /$_/; }