my %search; @search{ @keys2 } = undef; my @found; foreach my $key (@keys1) { push @found => $key if exists $search{$key}; }