in reply to Logical operations on arrays
If the order of the arrays makes no difference, it sounds like you are looking for an intersection operator of some sort. Array::Utils might have what you are looking for. I found this by searching metacpan for "union intersect".
Otherwise, a method to find them is pretty straightforward:
- configure a hash for the first array (*)
This configures a hash %hash1 to have a true value for all values found in @array1. (* - This is probably clearer as@hash1{@array1} = (1)x@array1$hash1{$_} = 1 for @array1) - loop through the second array picking out those elements that show up in the first arraypush( @intersection, grep { $hash1{$_} } @array2 );
Updates:
- Added example code
- Added search example
- D'oh - quite right Jenda. I have got to stop writing code before my first cup of coffee. :-/
--MidLifeXis
In Section
Seekers of Perl Wisdom