I have two arrays, @a
and @b
. I want to do a compare among the elements of the two arrays.
my @a = qw"abc def efg ghy klm ghn";
my @b = qw"def ghy jgk lom com klm";
If any element matches then set a flag. Is there any simple way to do this?
I have two arrays, @a
and @b
. I want to do a compare among the elements of the two arrays.
my @a = qw"abc def efg ghy klm ghn";
my @b = qw"def ghy jgk lom com klm";
If any element matches then set a flag. Is there any simple way to do this?
This question still could mean two things where it states "If any element matches then set a flag":
For case 1, you might do this:
For case 2, you might do that:
Note that in the first case, @matches holds the indexes of where there are matching elements, and in the second case @matches holds the matching values in the order in which they appear in @b.
This is one way:
List::Compare
From the requirement that 'if any element matches', use the intersection of sets:
Check to create an intersect function, which will return a list of items that are present in both lists. Then your return value is dependent on the number of items in the intersected list.
You can easily find on the web the best implementation of intersect for Perl. I remember looking for it a few years ago.
Here's what I found :
This is Perl. The 'obvious' solution:
given "\0" not being in @a.
But thanks for confirming that there is no other generic solution than rolling your own.