So I have an array my @a = (a,b,c,d,e,f)
And another array my @b = (c,d,e)
I want to find if there are three consecutive elements in @a that match those in @b.
Then, if there are, I want to get the indices those elements live in.
So in the case above I want to get an array such as (2,3,4).
Another example:
my @a = (1,2,3,4,5)
my @b = (2,3)
Output: (1,2)
Here is a general solution. This uses the ‘all’ function, from List::MoreUtils to reduce the comparison to a true/false result, which simplifies the logic a little.
I put it into the form of a sub to which you pass a reference to any two arrays. The first array ref passed to the function should be the superset, and the second array ref should refer to the subset array.
What I like about this solution is that it can apply to any two simple arrays (it isn’t constrained to seeking a two-element subset, for example). I did choose a string comparison of the elements (eq) as opposed to numeric (==). That way it works if you have non-numeric elements. However, it will evaluate ’00’ and ‘0’ as non-equal (because they’re not the same string). If you favor a numeric comparison, just find the ‘eq’ and change it to ‘==’.
Here’s the code: