Reading your post it struck me that you might have been trying not just to shuffle the elements within the nested arrays, and the order of the nested arrays with the top level, but possibly wanted to shuffle the elements between the arrays?

I seriously doubt that this is what you wanted to acheive, but it struck me as an interesting intellectual challenge none the less, and so I offer it on that basis:)

#! perl -slw use strict; use Data::Dumper; sub shuffleRefs (\@) { my $r=pop; $a = $_ + rand @{$r} - $_ and (${$r->[$_]}, ${$r->[$a]}) = (${$r->[$a]}, ${$r->[$_]}) for (0..$#{$r}); } sub shuffle2d { my @refs; push @refs, \( @$_ ) for @_; shuffleRefs @refs; return @refs; } my @data2d = ( [ qw[a b c d e] ], [ qw[1 2 3] ], [ qw[A B C] ], [ qw[I + II III IV] ] ); print "@$_" for @data2d; shuffle2d @data2d; print ''; print "@$_" for @data2d; __END__ C:\test>temp a b c d e 1 2 3 A B C I II III IV 1 a b e II d I c 2 C III A B 3 IV C:\test>

The interesting thing to note here is that whilst the elements have been shuffled between the nested arrays with proper statistical chance of any given element of any array ending up in any position in its original array or any other, but the size of each of the arrays can be different and the resultant arrays will remain the same size as they started out.

The trick is to shuffle a flattened array of references to the array elements and swap those elements through the references.

Now all I have to do is find a use for it:0


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

In reply to Re: How to Shuffle Multidimensional Arrays? by BrowserUk
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.