#!/usr/bin/perl use strict; use warnings; my @a = qw( a b c ); my @b = qw( a b c d ); # Get the individual differences. my @a_minus_b = setminus(\@a, \@b); my @b_minus_a = setminus(\@b, \@a); # merge then together and remove possible duplicates my @symmetric_difference = uniq(@a_minus_b, @b_minus_a); # Present our results. print 'List A: ', join(', ', @a), "\nList B: ", join(', ', @b), "\nA \\ B: ", join(', ', @a_minus_b), "\nB \\ A: ", join(', ', @b_minus_a), "\nSymmetric difference: ", join(', ', @symmetric_difference), "\n"; # Takes two array references. Returns a list of elements in the first # array that aren't in the second. sub setminus { my ($a, $b) = @_; # Convert $b to hash keys, so it's easier to search. my %b; @b{@$b} = (); return grep !exists $b{$_}, @$a; } # take a list and return only uniq items sub uniq { my %saw; return grep !$saw{$_}++, @_; }