SPUG: create a array which is XOR of two arrays

John W. Krahn krahnj at telus.net
Tue Oct 24 22:52:16 PDT 2006


Creede Lambard wrote:
> 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.

Or you could do it without the loops:

my %hash;
@hash{ @array2 } = ();
delete @hash{ @array1 };

@final = keys %hash;



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