use strict; use warnings; my @a = qw(foo bar baz); my @b = qw(foo bar biz); foreach my $temp (@a){ my @matches = grep {$temp eq $_} @b; print "Matches for $temp: ", join(", ", @matches), "\n" if @matches; }