Does $pick contain a reference to the anonymous subroutine, and it also remembers what was in my %kmer_prob hash?
Yes, & yes.
I suggest that you read section 4 of Perlref - Making references, carefully.
But, in a nutshell, when you create a subroutine (named or anonymous) it 'remembers' any variables external to that subroutine that it references.
So, this subroutine:
my $var = 2; sub x{ print $var; } x(); # prints 2 $var = 3; x(); # prints 3
For safety, you should isolate closed-over variables so they cannot be changed:
{ my $var = 2; sub x{ print $var }; } ## $var goes out of scope; but x() remembers it. x(); # prints 2; # $var = 3; ## Would be an error my $var = 3; x(); ## Still prints 2; the $var above is a different $var.
In reply to Re^13: an algorithm to randomly pick items that are present at different frequencies
by BrowserUk
in thread an algorithm to randomly pick items that are present at different frequencies
by efoss
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |