I am using the good old fisher-yates shuffle from perlfaq4:
my $ref = \@list;
for (my $i = @$ref; --$i; ) {
my $j = int rand ($i+1);
next if $i == $j;
@$ref[$i,$j] = @$ref[$j,$i];
}
# @list is modified in place
Question: why do I have to use a reference? I can't figure this out. I tried:
for (my $i = scalar(@list); --$i; ) {
my $j = int rand ($i+1);
next if $i == $j;
@list[$i,$j] = @list[$j,$i];
}
This didn't work... Some elements were duplicated in the changed list. This is probably just a lack of understanding references well, but could some monk please explain this?
</ajdelore>