my %onedir = map { $_ => undef } grep /^\w/, readdir(DIR1); my %twodir = map { $_ => undef } grep /^\w/, readdir(DIR2); for my $file ( sort keys %onedir ) { next unless ( exists( %twodir{$file} )); Calc( "$dir1/$file", "$dir2/$file", $testfile ); }