in reply to How to Shuffle Multidimensional Arrays?

First something that works (not that you weren't close, but I had written it before I realized how close you were.):
use strict; use warnings; my @AoA = ( [0..13], [0..11], [0..11] ); fisher_yates_shuffle(@AoA); foreach ( @AoA ) { print join ",",@$_; print $/; } sub fisher_yates_shuffle { while (my $deck = shift ) { my $i = @$deck; while ($i--) { my $j = int rand ($i+1); @$deck[$i,$j] = @$deck[$j,$i]; } } }
Reasons things don't work:
my @weeks = ('@week01','@week02','@week03'); for my $ref (@weeks) { for ($i = @$ref; --$i;) { my $r = int rand ($i+1); @$ref[$i, $r] = @$ref[$r, $i]; } } for(@weeks){ print; }
You get this result back when you try to run it:

Modification of non-creatable array value attempted, subscript -1 at E:\shuffle.pl

By entering the elements of the array with single quotes ( ie, '@week01' ) you don't even get the interpolated values of @week01, but a string @week01, so later on @$ref does not do what you expect.

The second one is closer but since you are escaping the @ in the arrays inside quotes you (ie "\@week01") you end up with the same type of deal as the first one. it would be closer to what you want as (note it is better to move line 2 further down):

my @week01 = (0..13); my @week02 = (0..11); my @week03 = (0..11); my @weeks_refs = (\@week01,\@week02,\@week03); for my $ref (@weeks_refs) { for ($i = @$ref; --$i;) { my $r = int rand ($i+1); @$ref[$i, $r] = @$ref[$r, $i]; } } for(@weeks_refs){ print join ",",@$_; print $/; }
As for the last one works you just have to dereference the arrays when you print them, that is:
for my $array_ref (@weeks_arr){ print join "," ,@{$array_ref}; }
or the shorter:
for(@weeks_arr) { print join ",",@$_; print $/; }
hope this helps. I think reading over perllol, perlref,perldsc,will help you get a better grasp of what is going on (i did not go that far into detail).

-enlil