my %in_array_b; @in_array_b{@array_b} = (); foreach my $one (@array_a) { print $in_array_b{$one} ? $hash_a{$one} : $hash_b{one}; }