SPUG: create a array which is XOR of two arrays

Joshua Keroes ua at pobox.com
Mon Oct 23 14:34:49 PDT 2006

On 10/23/06 12:41 AM, "Sachin Chaturvedi" <sachin_chat at coolgoose.com> wrote:
> i have two arrays { t1, t2 , t3 }, { t1 , t2 , t5, t7, t8}
> i want to create a new array which has all elements of array2 such that they
> are not in array1, e.g. {t5,t7,t8}

#!/usr/bin/perl -l

use strict;
use warnings;
use Set::Scalar;

my $s = Set::Scalar->new( qw[t1 t2 t3] );
my $t = Set::Scalar->new( qw[t1 t2 t5 t7 t8] );

# Take symmetric_difference()/xor/% of both sets
print "$s xor $t = " . ($s % $t);

# ...but I don't think you want a true xor, because t3 is present in
the results.
# If you want all elements in $t that are not in $s, try:
print "$t intersect $s xor $t = " . ($t * $s % $t);



(t1 t2 t3) xor (t1 t2 t5 t7 t8) = (t3 t5 t7 t8)
(t1 t2 t5 t7 t8) intersect (t1 t2 t3) xor (t1 t2 t5 t7 t8) = (t5 t7 t8)

More information about the spug-list mailing list