SPUG: create a array which is XOR of two arrays

Creede Lambard creede at penguinsinthenight.com
Tue Oct 24 23:41:42 PDT 2006


By golly, you learn something new every day if you're not careful.

John W. Krahn wrote:
> 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
>   



More information about the spug-list mailing list