A quick
perldoc -q shuffle array gives:
How do I shuffle an array randomly?
Use this:
# 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
Other points to consider:
- What on earth are you trying to do to $#arr?
- You probably don't want to use @arr in the subsitution
- You probably don't want to reinvent the wheel when there are perfectly good templating modules such as HTML::Template and Text::Template
- splice wants an integer offset, rand returns a fractional number
- print $_ is the same as print
I'm sure I've missed a few things. Sorry to sound overly harsh but I'm trying to be constructive :)
gav^