Biostar Beta. Not for public use.
Get identical elements and index from 2 Arrays in Perl
0
Entering edit mode
12 months 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

ADD COMMENTlink
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 REPLYlink
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 REPLYlink
0
Entering edit mode

Is this a homework/assignment question?

ADD REPLYlink
3
Entering edit mode
11 months ago
JC 7.9k
Mexico

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 COMMENTlink
0
Entering edit mode
12 months 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 COMMENTlink

Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.1