The sub keyword will only define the subroutine once, and to be able to write on @flankseq it will hold a reference to the variable (because otherwise, @flankseq being lexical, it could be destroyed although the function always needs it). So while you create a new @flankseq in each loop, the function always uses the same one. This is demonstrated by:

$\ = $/; for (1..2) { my $var = 1; print \$var, " created"; sub function { print \$var; } function(); }
SCALAR(0x825d70) created SCALAR(0x825d70) SCALAR(0x96ec08) created SCALAR(0x825d70)
The solution is to define seqstore outside of the loop (it's not actually required, it's just better practice) and pass the array as a paramater (as a reference, so that it is passed all at once).
sub seqstore { my ($flankseq, $mode, @rcd) = @_; if ($mode eq "Pos") { push (@$flankseq, $rcd[0]); } elsif ($mode eq "Freq") { push (@$flankseq, $rcd[0]) for (1..$rcd[1]); } return; }
Now you have to call the function like that: seqstore(\@flankseq, $mode, $seqEX,$F[2]);

More info on closures, and on references


In reply to Re: Array ending up empty when processing more than a single file by Eily
in thread Array ending up empty when processing more than a single file by TJCooper

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.