Dear Monks, I am new in Perl and I am trying to solve the following comparison. I have two arrays:
@A = ("Red", "Green", "Yellow"); @B = ("Yellow", "Black","Yellow","Red", "White", "Yellow");
In array A, each element occurs only once.
In array B, each element can occur zero, one or many times.
For every element in A, the code should lists the position at which it is present in B and give the output as following:
> Red at index 3 > Green is missing > Yellow at index 0, 2 and 5 > Elements from B were detected 4 times in A
I tried the following script but I cannot figure out how to list indexes of elements after comparing two arrays
foreach $x (@A){ foreach $y (@B){ if ($y eq $x){ print "$y\n"; } elsif ($x ne$y){ print "$x"; } } }
Can someone please, help me? Thank you very much in advance! Rebi
In reply to Get identical elements and index from 2 Arrays in Perl by rebkirl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |