my @a = (0,1,2,3,4,5, 6, 7, 8 ); my @b = (0,2,4,6,8,10,12,14,16); my (@matches,@mismatch); print "An iterative solution\n"; foreach $a (@a) { push @matches, $a if grep{$a == $_}@b; } foreach $a (@a) { push @mismatch, $a unless grep{$a == $_}@b; } print "The arrays share: @matches\n"; print "Unique elements in \@a: @mismatch\n"; print"\nA hash solution\n"; @matches = @mismatch = (); map{$seen{$_}++}@b; @matches = grep{defined $seen{$_}}@a; @mismatch = grep{!defined $seen{$_}}@a; print "The arrays share: @matches\n"; print "Unique elements in \@a: @mismatch\n"; #### use Benchmark; $build = <<'BUILD'; my @a = (0..10000); my @b = (9000..19000); BUILD $iterate = <<'ITERATE'; my @a = (0..10000); my @b = (9000..19000); my (@matches,@mismatch); @matches = @mismatch = (); foreach $a (@a) { push @matches, $a if grep{$a == $_}@b; } foreach $a (@a) { push @mismatch, $a unless grep{$a == $_}@b; } ITERATE $hash = <<'HASH'; my @a = (0..10000); my @b = (9000..19000); my (@matches,@mismatch); @matches = @mismatch = (); map{$seen{$_}++}@b; @matches = grep{defined $seen{$_}}@a; @mismatch = grep{!defined $seen{$_}}@a; HASH timethese(1,{ 'build' => $build, 'iterate' => $iterate, 'hash' => $hash }); __END__ C:\>perl test.pl Benchmark: timing 1 iterations of build, hash, iterate... build: -1 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) (warning: too few iterations for a reliable count) hash: 0 wallclock secs ( 0.16 usr + 0.00 sys = 0.16 CPU) @ 6.25/s (n=1 ) (warning: too few iterations for a reliable count) iterate: 147 wallclock secs (147.20 usr + 0.00 sys = 147.20 CPU) @ 0.01/s ( n=1) (warning: too few iterations for a reliable count) C:\>