[sf-perl] randomize particular lines
qw at sf.pm.org
Tue Mar 7 09:42:44 PST 2006
On Mon, Mar 06, 2006 at 08:03:48PM -0800, Fred Moyer wrote:
> I had to get a random index from a set last week in several places in my
> code. I was using '$word = $words[int(rand(@words))]' (wrapped in a
> subroutine call), but it's a fairly noisy idiom and I ended up using
> '$word = $words[rand(@words)]' as it's a bit easier on the eyes.
> Quinn can you explain a bit more how this expression can return undef?
> I'm using it with a defined @words list and haven't run into any
> problems yet.
Re-running the relevant test, I believe I was mistaken. It tried
index 2.2 on a two-element array. Oops.
The result is undef, but that's probably the least illogical outcome in
this case. Anyway, you wouldn't get an out-of-range value like that
from your use of rand above.
qw (Quinn Weaver); #President, San Francisco Perl Mongers
=for information, visit http://sf.pm.org/weblog =cut
More information about the SanFrancisco-pm