The canonical way to choose one element randomly from an array
@a is
$item = $a[ rand @a ];
If you need to do this n times, you could therefore do this:
my @items;
for ( 1 .. $n )
{
push @items, $a[ rand @a ];
}
However, you have not said whether you can validly choose the same value twice.
If not, then you'll have to remove each element from the set when you choose it.
Like so:
my @items;
for ( 1 .. $n )
{
push @items, splice @a, rand @a, 1;
}
Of course, if you'd prefer not to destroy @a, then work on a disposable copy of it.
jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.