Whether this will work correctly will depend upon how you are constructing your 1D array. Or rather, the source from which you are constructing it. Ie. If you are reading the data in from a file dumped from FORTRAN, you may need to invert the X and Z dimensions of the data.

I vaguely remember from my FORTRAN days, that the ordering of multi-dimensional array elements in FORTRAN is different from that in C. And I have no idea how that will translate to Perl's arrays.

#! perl -slw use strict; use Data::Dump qw[ pp ]; my @D1 = split ' ', do{ local $/; <DATA> }; pp \@D1; my @D3 = map{ my $z = $_; [ map{ my $y = $_; [ @D1[ $z*9 + $y*3 .. $z*9 + $y*3 + 2 ] ] } 0 .. 2 ] } 0 .. 2; pp \@D3; __DATA__ z0y0x0 z0y0x1 z0y0x2 z0y1x0 z0y1x1 z0y1x2 z0y2x0 z0y2x1 z0y2x2 z1y0x0 z1y0x1 z1y0x2 z1y1x0 z1y1x1 z1y1x2 z1y2x0 z1y2z1 z1y2x2 z2y0x0 z1y0x1 z2y0x2 z2y1x0 z2y1x1 z2y1x2 z2y2x0 z2y2x1 z2y2x2

Output:

c:\test>754307.pl [ "z0y0x0", "z0y0x1", "z0y0x2", "z0y1x0", "z0y1x1", "z0y1x2", "z0y2x0", "z0y2x1", "z0y2x2", "z1y0x0", "z1y0x1", "z1y0x2", "z1y1x0", "z1y1x1", "z1y1x2", "z1y2x0", "z1y2z1", "z1y2x2", "z2y0x0", "z1y0x1", "z2y0x2", "z2y1x0", "z2y1x1", "z2y1x2", "z2y2x0", "z2y2x1", "z2y2x2", ] [ [ ["z0y0x0", "z0y0x1", "z0y0x2"], ["z0y1x0", "z0y1x1", "z0y1x2"], ["z0y2x0", "z0y2x1", "z0y2x2"], ], [ ["z1y0x0", "z1y0x1", "z1y0x2"], ["z1y1x0", "z1y1x1", "z1y1x2"], ["z1y2x0", "z1y2z1", "z1y2x2"], ], [ ["z2y0x0", "z1y0x1", "z2y0x2"], ["z2y1x0", "z2y1x1", "z2y1x2"], ["z2y2x0", "z2y2x1", "z2y2x2"], ], ]

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: 1d array into 3d by BrowserUk
in thread 1d array into 3d by MKevin

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.