in reply to syntax issues
$firstrandom = $list1[rand @list1]; push @list2, $firstrandom; $count = $#list1; while ($count != 0) { ###create list2 $random = $list1[rand @list1]; $ispresent = 'false'; for $I (0..$#list2){ ##check to see if team is already in lis +t2 if ($random == $list2[$I]) { $ispresent = 'true' }; }; if($ispresent eq 'false') { ##if not add it to @list2 push @list2, $random; $count--; }; };
You are building a randomized version of @list1. The most efficient way of randomly shaking up a list like this is the Fisher-Yates shuffle:
Fisher-Yates (from the Perl Cookbook from O'Reilly):
# fisher_yates_shuffle( \@array ) : generate a random permutation # of @array in place sub fisher_yates_shuffle { my $array = shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i,$j] = @$array[$j,$i]; } } fisher_yates_shuffle( \@array ); # permutes @array in place
|
|---|