SPUG: create a array which is XOR of two arrays

Creede Lambard creede at penguinsinthenight.com
Tue Oct 24 22:21:40 PDT 2006


The quick and dirty version:

#!/usr/bin/perl

@array1 = ( t1, t2 , t3 );
@array2 = ( t1 , t2 , t5, t7, t8);

my %hash = ();
foreach $element (@array2) { $hash{$element} = 1; }
foreach $element (@array1) { delete $hash{$element}; }

@final = keys %hash;

Someone is going to chew me out, and rightly so, for not using strict 
and -w.


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}
> _____________________________________________________________
> Seattle Perl Users Group Mailing List  
>      POST TO: spug-list at pm.org
> SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
>     MEETINGS: 3rd Tuesdays
>     WEB PAGE: http://seattleperl.org/
>   



More information about the spug-list mailing list