Another way of doing it that maybe slightly faster and use less memory than building a hash or grep. It would also work if the arrays contained duplicates or even compound objects instead of simple string or numbers.
#! perl -slw use strict; use constant ALLPRODUCTS =>0; use constant PROHIBITED =>1; my @allProducts = map { int rand 10_000 } 1..1000; my @selection = map { $allProducts[rand 1000] } 1..50; sub randProduct (\@\@) { local $"="\cA"; #!" my ($choice, $disallowed) = ('', "@{$_[PROHIBITED]}"); $choice = $_[ALLPRODUCTS]->[rand @{$_[ALLPRODUCTS]}] while 1+index +( $disallowed, $choice ); return $choice; } my $chosen = randProduct @allProducts, @selection;
Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.
In reply to Re: Choosing a random product from an Array
by BrowserUk
in thread Choosing a random product from an Array
by krisraman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |