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

    rebkirl:

    You can get both the index and the value from an array by iterating through it like this:

    $ cat x.pl use strict; use warnings; my @foo = qw( apple pear banana ); while (my ($x,$y) = each @foo) { print "<$x, $y>\n"; } $ perl x.pl <0, apple> <1, pear> <2, banana>

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Get identical elements and index from 2 Arrays in Perl
by Eily (Monsignor) on May 27, 2019 at 13:07 UTC

    That's one of the cases where it's easier (and actually required) to iterate over the indexes rather the elements themself. $#data is the index of the last element in the array @data:

    my @ref = qw(Red Green Yellow); my @data = qw(Yellow Black Yellow Red White Yellow); for my $search (@ref) { my @found; for my $index (0..$#data) { if ($data[$index] eq $search) { push @found, $index; } } print "$search: [@found]\n"; }
    Red: [3] Green: [] Yellow: [0 2 5]
    This should give you most of what you need for your task. Are you familiar with grep? If so it can be used to get the @found array in one line rather than with a explicit loop.

    Also, while it's kind of acceptable to not use strict when testing, you should always avoid using one letter names for variables. Mostly for clarity, but also because some of them might have some hidden side effect / behaviour.

      Thanks Eily. I am not familiar with the 'grep' feature, only loops and arrays structures. Is it possible to print strings for each output line "print "$search: @found\n";" ?. Thank you a lot!

        I'm sorry I did not get that question. I know that my code output is not exactly what you asked for, but the main logic is there. If you've already tried to adapt it to your needs, please show us your updated code, and what's missing for you to continue.

Re: Get identical elements and index from 2 Arrays in Perl
by hippo (Archbishop) on May 27, 2019 at 13:20 UTC

    Loop over the indexes in the inner loop instead of the values and push matches to a list.

    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my @A = ("Red", "Green", "Yellow"); my @B = ("Yellow", "Black","Yellow","Red", "White", "Yellow"); for my $x (@A){ my @matches; for my $y (0 .. $#B){ push @matches, $y if $B[$y] eq $x; } say $#matches < 0 ? "$x is missing" : "$x at index " . join ', ', +@matches; }
    I am new in Perl

    When you fancy something more advanced you can push into a hash of arrays (HoA) instead. That way you can store the results rather than just printing them out as you go. HTH.

      Thank you a lot. I learned vey basic codes and not yet seen feature 'say' and 'HoA' but it could be useful for next steps!
Re: Get identical elements and index from 2 Arrays in Perl
by LanX (Saint) on May 27, 2019 at 13:08 UTC
    > figure out how to list indexes of elements after comparing two arrays

    The easiest way is to increment a variable $index++ inside your @B loop.

    Please note that swapping the loops would make sense here.

    The most efficient way though would be using a hash %A to lookup set elements in @A thus avoiding that loop.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Get identical elements and index from 2 Arrays in Perl
by choroba (Cardinal) on May 27, 2019 at 14:50 UTC
    Crossposted to StackOverflow.

    It's considered polite to inform about crossposting to prevent people not attending both sites from unnecessary work (i.e. solving a problem already solved at the other side of the internets).

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]