Get identical elements and index from 2 Arrays in Perl
3
0
Entering edit mode
5.5 years ago
rebkirl • 0

Dear All, 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 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

perl arrays index • 4.6k views
ADD COMMENT
0
Entering edit mode

Have a look in the PERL cpan libs, there are librairies to work with arrays and one of them is to intersect arrays

ADD REPLY
0
Entering edit mode

Hi ,

Your post don't have links with bio-informatics you should post it on stackoverflow.

I still give you a link where you can find what you are looking for Perl tuto.

ADD REPLY
0
Entering edit mode

Is this a homework/assignment question?

ADD REPLY
3
Entering edit mode
5.5 years ago
JC 13k

Comparing arrays is easy using a variable for indexes, also you will need other variables to record every successful comparison:

#!/usr/bin/perl

use strict;
use warnings;

my @A = ("Red", "Green", "Yellow");
my @B = ("Yellow", "Black","Yellow","Red", "White", "Yellow");

my $tot = 0;  # To store all matches
for (my $x=0; $x<=$#A; $x++) { # iterate over A positions
    my $found = "no";
    my @pos = ();
    for (my $y=0; $y<=$#B; $y++) { # iterate over B positions
        if ($B[$y] eq $A[$x]) { # Compare A[i] vs B[i]
            $tot++;
            $found = "yes";
            push(@pos, $y);
        }
    }
    if ($found eq "yes") {
        print "$A[$x] at index ", join (", ", @pos), ".\n";
    }
    else {
        print "$A[$x] is missing.\n";
    }
}

print "Elements from B were detected $tot times in A.\n";

Executing the code:

$ perl array.pl
Red at index 3.
Green is missing.
Yellow at index 0, 2, 5.
Elements from B were detected 4 times in A.
ADD COMMENT
0
Entering edit mode
5.5 years ago
#!/usr/bin/perl -w
use strict;
my @A = ("Red", "Green", "Yellow");
my @B = ("Yellow", "Black","Yellow","Red", "White", "Yellow");

my %hash;
foreach my $k (@A) {
              $hash{$k} = [];
              foreach (0 .. $#B) {
                     push @{$hash{$k}},$_ if $k eq $B[$_];
               }
}

my $count;
foreach (keys %hash) {
       print "$_\t";
       print "is missing at B\n" if @{$hash{$_}} == 0;
       print "INDEX at B is @{$hash{$_}}\n" if @{$hash{$_}} >= 1;
       $count += scalar @{$hash{$_}};
}

print "A have $count Elements at B\n";

output

Red INDEX at B is 3

Green is missing at B

Yellow INDEX at B is 0 2 5

A have 4 Elements at B

ADD COMMENT
0
Entering edit mode
5.3 years ago
The ▴ 180
use strict;
use warnings;
my @A = ("Red", "Green", "Yellow");  
my @B = ("Yellow", "Black","Yellow","Red", "White", "Yellow");
foreach my $col (@A){
    my @hit_indices = map{$B[$_] eq $col  ? $_: () }(0..$#B);
    scalar(@hit_indices) ? print $col, " at indices ",join(',',@hit_indices),".\n" :
                           print $col, " is missing.\n";

}
ADD COMMENT

Login before adding your answer.

Traffic: 2119 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6