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


In reply to Re: How to Shuffle Multidimensional Arrays? by Enlil
in thread How to Shuffle Multidimensional Arrays? by Withigo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.