Question: Get identical elements and index from 2 Arrays in Perl
0
gravatar for rebkirl
3 months ago by
rebkirl0
rebkirl0 wrote:

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

index arrays perl • 241 views
ADD COMMENTlink modified 11 days ago by The160 • written 3 months ago by rebkirl0

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 modified 3 months ago • written 3 months ago by lieven.sterck5.6k

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 written 3 months ago by Titus890

Is this a homework/assignment question?

ADD REPLYlink written 3 months ago by jrj.healey13k
3
gravatar for JC
3 months ago by
JC8.2k
Mexico
JC8.2k wrote:

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 written 3 months ago by JC8.2k
0
gravatar for chanbioinformatics
11 weeks ago by
chanbioinformatics0 wrote:
#!/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 modified 11 weeks ago • written 11 weeks ago by chanbioinformatics0
0
gravatar for The
11 days ago by
The160
United States
The160 wrote:
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 COMMENTlink written 11 days ago by The160
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1669 users visited in the last hour