Assuming @one and @two have the same number of elements and that you wanted each element to be an arrayref...

@mda = map { [[$one[$_-1]], [$two[$_-1]]] } 1..@one;

Or maybe more likely you simply wanted this...

@mda = map { [$one[$_-1], $two[$_-1]] } 1..@one;

It would also be possible to create a tied array that looks up its values in the other arrays in real time, but that would be more complicated and probably has no advantage, unless you want changes to the original arrays to be reflected in the multidimensional one.

update: fixed subscripting fencepost error pointed out to me by revdiablo and L~R. And yes, if the arrays might not be the same length you can check which one is longer and use either the shorter or the longer one to determine the upper bound of the range operator, depending on which results you want (whether you want the arrayrefs that would have an undef element or not). But I got the impression from the question that they would necessarily be the same length.

Since nobody else has done so, I will also point out that the syntax the OP used for the desired structure is wrong. He has this (which of course is a syntax error):

@mda = ( [A][E], [B][F], [C][G], [D][H] );

It appears that what he wanted is this:

@mda = ( [A,E], [B,F], [C,G], [D,H], );

Though I personally would have used the funny comma (=>) within each of those arrayrefs especially if there are exactly two elements in each. But that doesn't technically matter, I think.


$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

In reply to Re: build multidimensional array from arrays by jonadab
in thread build multidimensional array from arrays by kevsurf

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.