foreach my $file_name (@array) { open FILE, "$file_name" or die "File not exists"; while ( ) { print OUTPUT $_ if /^(.*?,.*?),/ and exists $records{$1}; } }