If you wish to loop through an array, you probably want a for loop, not a while loop.
for my $target (@Accession) { }
While inside the for loop, you can then grep out any rows that match from @GI:
my @found = grep /$match/, @GI;
More than likely you only ever match one element, but you should be thinking about the notion that you might find multiples. Once you find your match(es), you will then most likely want to split the record(s) up. Put the whole thing together and you have something like this:
use strict; use warnings; use Data::Dumper; my @GI = ( 'gi|Q384722390|emb|WP_938420210.1|Gene name', 'gi|342546780|emb|WP_934203412.1|Gene name', ); my @Accession = ( 'WP_938420210.1', 'WP_934203412.1' ); for my $match (@Accession) { my @found = grep /$match/, @GI; print "$match\n", Dumper [ split /\|/, $_ ] for @found; }
UPDATE: You know ... since you already have all that data within @GI and it is already in memory, you could ease the problem by using a HoA (hash of arrays) instead of an Array that essentially contains unparsed CSV lines. Once you have this HoA, you simply lookup your accession value. The following code should demonstrate and notice that i am taking the liberty to rename these variables:
Now then ... i hope that really helps! :)use strict; use warnings; use Data::Dumper; my @genes = ( 'gi|Q384722390|emb|WP_938420210.1|Gene name', 'gi|342546780|emb|WP_934203412.1|Gene name', ); my @accession = ( 'WP_938420210.1', 'WP_934203412.1' ); my %genes = map { my @s = split /\|/, $_; ( $s[3] => [@s] ); } @genes; print Dumper \%genes; # list the 2nd field from each gene for (@accession) { print $genes{$_}[1], $/; }
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
In reply to Re: Using regular expressions with arrays
by jeffa
in thread Using regular expressions with arrays
by andybshaker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |