my %onefile= map { $_ => 1 } readdir(TMP); # Now every file in TMP is a key in the hash ... # and later we check like this: foreach $file (@allfiles) { if ($onefile{$file}) ...