Here's my version that's a little more perlish as you'd say.:

use Data::Dumper; use strict; use warnings; sub pivot { my @src = @_; my $max_col = 0; $max_col < $#$_ and $max_col = $#$_ for @src; my @dest; for my $col (0..$max_col) { my @new_row; for my $row (0..$#src) { push @new_row, $src[$row][$col] // ''; } push @dest, \@new_row; } return @dest; } my @test = ( [1..5], [11..15], [21..25], [31..35], [41], [51..55], ); my @results = pivot(@test); print Dumper(\@results); =prints $VAR1 = [ [1,11,21,31,41,51], [2,12,22,32,'',52], [3,13,23,33,'',53], [4,14,24,34,'',54], [5,15,25,35,'',55] ]; =cut

Update: And a little more perlish still would be to use map:

sub pivot { my @src = @_; my $max_col = 0; $max_col < $#$_ and $max_col = $#$_ for @src; my @dest; for my $col (0..$max_col) { push @dest, [map {$src[$_][$col] // ''} (0..$#src)]; } return @dest; }

This last is actually my preferred way of doing this, since it's easy to see what the inner loop is iterating on.

- Miller

In reply to Re: Pivoting 2 dimensional array refs by wind
in thread Transposing 2 dimensional arrays by Voronich

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.