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:

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], $/; }
Now then ... i hope that really helps! :)

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.