use strict; use warnings; chomp(@ID); foreach my $ID(@ID) { foreach my $file (@files){ if ($ID=~ /$file/) { print "$file\n"; #if the matches are unique, you can bail this foreach after a match last; } } }