SPUG: create a array which is XOR of two arrays

John W. Krahn krahnj at telus.net
Mon Oct 23 00:40:40 PDT 2006


Sachin Chaturvedi wrote:
> i have two arrays
> @array1 = { t1, t2 , t3 }
> @array2 = ( 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.
> i mean my final array should be
> @final = {t5,t7,t8}

$ perl -le'
my @array1 = qw{ t1 t2 t3 };
my @array2 = qw( t1 t2 t5 t7 t8 );

my @final = do {
    my %temp = map { $_, 1 } @array1;
    grep !$temp{ $_ }++, @array2;
    };

print "@final";
'
t5 t7 t8



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall


More information about the spug-list mailing list