in reply to Re: Re: Choosing a random product from an Array
in thread Choosing a random product from an Array
Update: I'd just thought to clarify why the hash was called %valid. While it's true that it contains valid and invalid members, the purpose of the hash is to store the valid status of the entry. Obscure, perhaps, oblique, maybe, but functional.use warnings; use strict; my @first = qw( 1 3 5 7 9 11 13 15 17 19 21 23 ); my @second = qw( 1 5 9 13 17 21 ); sub random_from { my ($first, $second) = @_; my %invalid = (map { $_ => 1 } @$second); my $return; do { # Choose a random key from the combined listing $return = $first->[rand(@$first)]; } while ($invalid{$return}); return $return; } print "Selection: ",random_from(\@first, \@second),$/;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re^3: Choosing a random product from an Array
by sauoq (Abbot) on Nov 17, 2002 at 02:47 UTC |