my %seen; $seen{$_}++ for @return, @remoteFilelist; my @files_in_both_lists = grep $seen{$_} > 1, keys %seen; for my $fname (@files_in_both_lists) { if (compare_text("$path1/$fname", "$path2/$fname") == 0) { #... } }