[sf-perl] a reason to goto
Joe Brenner
doom at kzsu.stanford.edu
Sat Feb 24 23:25:23 PST 2007
David Alban wrote:
> What about making a subroutine out of it, to which you pass $REFLEXIVE (and
> maybe $i and $j)? If after calling pick_numeric( ... ) $REFLEXIVE doesn't
> have the value you want, you recursively call the subroutine? I know some
> folks don't like recursion, and if you anticipate a large number of
> recursive calls it could be a performance issue, but I thought I'd ask.
I don't have any particular dislike of recursion [1] -- except that I find
that it *still* takes me forever to debug recursive code -- but I
probably wouldn't resort to it for something like this. The code as
written strikes me as fairly straight-forward, it's just that perl's
quirks were getting in the way of getting it working.
In case it's not clear, the idea is simply that I want to randomly
choose two integers, but under normal conditions I'd rather that
they not be the same numbers (however if $REFLEXIVE is set, then
two identical picks are okay).
I don't know if it made it out to the list (I think my mail is acting
flaky at the moment) but the perlish way of doing it is probably this:
if ($RANDOMIZE) {
{{
$i = pick_numeric(0, $numb_cards-1);
$j = pick_numeric(0, $numb_cards-1);
unless( $REFLEXIVE ) {
if ($i == $j) {
redo;
}
}
}}
}
Though if "pick_numeric" were an expensive function, I might prefer
not to redo both picks, and I'd go for *something* like this:
if ($RANDOMIZE) {
{
$i = pick_numeric(0, $numb_cards-1);
do {
$j = pick_numeric(0, $numb_cards-1);
} until ($j != $i) || ($REFLEXIVE);
}
}
[1] As for worrying about disk thrashing: it's not particuarly difficult
to use a counter to error out if you've exceeded 10000 levels of recursion
or so.
More information about the SanFrancisco-pm
mailing list