in reply to Re: Matching arrays with different number of elements and element size
in thread Matching arrays with different number of elements and element size
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Matching arrays with different number of elements and element size
by thanos1983 (Parson) on Jul 13, 2017 at 15:19 UTC | |
by Ksonar (Initiate) on Jul 14, 2017 at 04:40 UTC |