in reply to shuffle a text file

Your shuffle() implementation shuffles the array in-place; in other words, it modifies the array passed in as a parameter. It then returns something that isn't useful; the Boolean result of the final for-loop (keep-going?) test. It never returns a shuffled array because you never tell it to return the shuffled array.

You probably wanted something like this:

sub shuffle { my @array = @{shift()}; return unless @array; # Cannot be empty. my $i = @array; while( --$i ) { my $j = int rand($i+1); @array[$i,$j] = @array[$j,$i]; } return @array; }

This implementation makes a copy and shuffles it. It then returns that shuffled copy of the original array, leaving the original in-tact. If an in-place shuffle is ok, then do that, but don't return anything. Just expect that the parameter will have been modified.

But then there's really no reason to do that when a perfectly good implementation of the Fisher Yates shuffle already comes with Perl: List::Util has List::Util::shuffle.


Dave