foreach my $files(@files) { $c++; if($files=~m|.*/([^_]+)|) { $f_name = $1; } if (exists $filelist{$f_name}) { print "$c - Found and to be deleted: $files\n"; } }