@mammals = qw/dog cat mouse horse planet/; @sky = qw/star planet galaxy nova comet/; %h = map {$_ => $h{$_}++} @mammals, @sky; foreach $k (sort keys %h){ print "in both: $k\n" if $h{$k}; }