Hi BrowserUk,

I have another question about your code, specifically about this piece:

return sub { my $r = rand(); $r < $odds[ $_ ] and return $vals[ $_ ] for 0 .. $#odds; };

I described my problem as having a list of values - A, B, C, etc. - and relative odds corresponding to choosing those values. I have tried to incorporate your code into a script in which I pass a %vals_odds hash to your genPicker subroutine. Things seem to go well until the return statement, but then nothing is returned with this statement in the main body of my script:

my $pick = genPickerConverted(\%kmer_prob);

And if I step through the code, right before I would enter the "return" block above, my @odds array has cumulative relative odds in it (so it ends with a 1, as it should), but then it never enters the "return" block.

My understanding of the return block (which looked fairly foreign to me when I saw it) is as follows:

# return something that is going to come from ... # ... an unnamed subroutine (unnamed ... # ... because there's nothing between "sub" and "{" return sub { # r is a random number >= 0 < 1 my $r = rand(); # an implicit if statement: # if, when going through every value of odds from lowest ... # ... to highest, r is less than that value of odds, this ... # ... code will go on to the "and" statement, and otherwise... # ... it will go on to the next value in @odds # if it gets to the "and" statement, it will return the ... # ... corresponding value for @vals to the subroutine ... # ... call, which will, in turn, return that to the main ... # body of the script $r < $odds[ $_ ] and return $vals[ $_ ] for 0 .. $#odds; };

Is my understanding of the "return" block correct? And why does my code not get in there when I pass my vals and odds to the subroutine as a hash rather than a file handle? (I've changed the code so that I can tell that my hash gets in there correctly and is converted to @vals and @odds as I expect.) I'd include more code except that it gets really long and, I think, just adds confusion to my question.

Thanks.

Eric


In reply to Re^8: an algorithm to randomly pick items that are present at different frequencies by efoss
in thread an algorithm to randomly pick items that are present at different frequencies by efoss

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.