in reply to Array ending up empty when processing more than a single file
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(); }
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).SCALAR(0x825d70) created SCALAR(0x825d70) SCALAR(0x96ec08) created SCALAR(0x825d70)
Now you have to call the function like that: seqstore(\@flankseq, $mode, $seqEX,$F[2]);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; }
More info on closures, and on references
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Array ending up empty when processing more than a single file
by Your Mother (Archbishop) on Oct 05, 2017 at 16:12 UTC | |
by Eily (Monsignor) on Oct 05, 2017 at 16:44 UTC | |
by LanX (Saint) on Oct 05, 2017 at 17:35 UTC | |
by Your Mother (Archbishop) on Oct 05, 2017 at 18:01 UTC | |
by Eily (Monsignor) on Oct 06, 2017 at 01:36 UTC | |
by LanX (Saint) on Oct 06, 2017 at 01:57 UTC | |
|