in reply to Matching arrays with different number of elements and element size

Hello Ksonar,

Welcome to the Monastery. Check this question (Difference between two arrays - is there a better way?), it contains all the answers to your problem.

Update: A few comments regarding the sample code that you provided.

When you compare if ( $array1[$j] =~ $array2[$k] ) you are not comparing the strings read perlop/DESCRIPTION. You should use eq for strings and == for integers. This is a binding operators =~ read more here perlop/Binding Operators.

One final note:

In Perl you do not need to loop arrays in C style for ( $j = 0 ; $j < $array1Size ; $j++ ) you can loop them through foreach my $element (@array) {} read more here perlop/Foreach Loops. In case you want to iterate over two equal size arrays you can do it like this foreach my $index ( 0 .. $#array ){} but in your case your arrays are not same in size so you can not use this method just for reference, read more here How do I Loop over multiple arrays simultaneously ?.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Matching arrays with different number of elements and element size
by Eily (Monsignor) on Jul 13, 2017 at 15:13 UTC

    Hello thanos1983. One precision on this:

    When you compare if ( $array1[$j] =~ $array2[$k] ) you are not comparing the strings read perlop/DESCRIPTION. You should use eq for strings and == for integers.
    It's mostly true, if you consider "compare" to only mean check that two strings are equal. As you said, the =~ will bind the left operand to the right, which will be interpreted as a regex (so this is the same as $array1[$j] =~ m/$array2[$k]/ if $array2[$k] is a string). When the string in the regex does not have meta characters (as is the case for all strings in the first array), this will actually check that the first string contains the second. Checking for inclusion may be considered one flavor of "comparing" two strings (and the test would be true when the strings are equal).

    Do note that the strings in the second array are longer than the ones in the first, so eq won't work. $array2[$k] =~ /$array1[$j]/ might work as it checks that the second string contains the first (notice that I have inverted the two), except if the string in @array1 contains metacharacters (in which case it can either give the wrong result, or just die). That's why my advice is to use index instead.

      Hello Eily,

      You are absolutely right. I did not notice that both arrays have different strings this is why I proposed eq but you are right he should using kind of a regex solution or grep.

      Thanks, it is nice to point out minor mistakes to avoid confusion for OP.

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        Hi Eily and thanos1983,

        Thank you very much. I have tried using eq but no luck.

        For this example the array might look like the substring of another array but the dataset I am working on is completely different, I will have to modify the base

        code and use it. tybalt89 gave a solution which I will try to understand and it is working on the dataset.