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 (*)
@hash1{@array1} = (1)x@array1
This configures a hash %hash1 to have a true value for all values found in @array1. (* - This is probably clearer as $hash1{$_} = 1 for @array1)
- loop through the second array picking out those elements that show up in the first array
push( @intersection, grep { $hash1{$_} } @array2 );
There are a couple of assumptions built into the above (@arrayN does not contain an undef value, and the second array only has unique values).
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. :-/