Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found