rebkirl has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Get identical elements and index from 2 Arrays in Perl
by roboticus (Chancellor) on May 27, 2019 at 13:11 UTC | |
|
Re: Get identical elements and index from 2 Arrays in Perl
by Eily (Monsignor) on May 27, 2019 at 13:07 UTC | |
by rebkirl (Acolyte) on May 27, 2019 at 14:29 UTC | |
by Eily (Monsignor) on May 27, 2019 at 14:51 UTC | |
|
Re: Get identical elements and index from 2 Arrays in Perl
by hippo (Archbishop) on May 27, 2019 at 13:20 UTC | |
by rebkirl (Acolyte) on May 27, 2019 at 14:24 UTC | |
|
Re: Get identical elements and index from 2 Arrays in Perl
by LanX (Saint) on May 27, 2019 at 13:08 UTC | |
|
Re: Get identical elements and index from 2 Arrays in Perl
by choroba (Cardinal) on May 27, 2019 at 14:50 UTC |