I'ts always seemed to me regrettable that List::MoreUtils chose to take the iterator approach to this. I find the transpose function a lot more useful:

sub transpose { map { my $i = $_; [ map $_->[ $i ], @_ ] } 0 .. $#{ $_[0] } } my @array1 = qw /ab bc cd de/; my @array2 = qw /cc dd ee gg/; my @array3 = qw /12 34 56 78/; print "@$_\n" for transpose \( @array1, @array2, @array3 ); __END__ ab cc 12 bc dd 34 cd ee 56 de gg 78
As shown above, iterating throught the transpose is as easy as iterating through any other array, and much more flexible (e.g. one can easily iterate through the rows in reverse order, etc.).

One handy use for the transpose is to feed multi-arg functions via map; e.g.:

my @firsts = ( 1, 2, 3, 4 ); my @seconds = qw( w x y z ); my @thirds = ( 5.6, 7.8, 9.0, 1.2 ); my @foos = map foo( @$_ ), transpose \( @firsts, @seconds, @thirds ); # @foos gets foo( 1, 'w', 5.6 ), foo( 2, 'x', 7.8 ), etc.
Plus, the transpose of a numerical matrix is a mathematically useful entity of its own right (though admittedly, one wouldn't use Perl AoA's for serious matrix-oriented computation, but rather something like PDL.)

the lowliest monk


In reply to Re^2: Printing multiple arrays as multiple column by tlm
in thread Printing multiple arrays as multiple column by monkfan

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.