If I understand right (from looking at your example code in the OP), you want a structure that looks like this:
@AoA = ( [ $MDA[0][0][0], $MDA[0][0][1], ... , $MDA[0][2][2] ], [ $MDA[1][0][0], $MDA[1][0][1], ... , $MDA[1][2][2] ], [ $MDA[2][0][0], $MDA[2][0][1], ... , $MDA[2][2][2] ], );
In other words, $AoA[x] contains all the elements of the form $MDA[x][*][*], if you follow my notation.

I don't see how this corresponds with your "Expected" data, but one thing I noticed is that you should make the following change:

# my $val = Dive( \@MDA, @indices ); # push @{$AoA[$level]}, @$val; my $val = Dive( $MDA[$level], @indices ); push @{$AoA[$level]}, $val;
Because when dealing with $AoA[$level], you only care about things starting form $MDA[$level]. Also, then $val will not be an arrayref like it was before.

There are many equivalent ways to write this, but another way is the following: Here you can replace the entire foreach loop with this:

NestedLoops( [ ( [0..$dimension-1] ) x ($depth-1) ], sub { my @indices = @_; my $val = Dive( \@MDA, @indices ); push @{ $AoA[$indices[0]] }, $val; } );
To explain this, it takes everything of the form $MDA[a][b][c] and pushes it onto the array at $AoA[a].

blokhead


In reply to Re^3: Unfolding a nDeep structure by blokhead
in thread Unfolding a nDeep structure by FFRANK

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.