If I read the text of your post correctly, you actually have a one-dimensional array and others have been confused by the output samples that represent LaTeX output. Here's a solution that avoids multi-dimensional arrays:

#!/usr/bin/perl use strict; use warnings; sub pa { print join(', ', @_), "\n" } die "usage: $0 <number of items> <number of columns>" unless $#ARGV == 1; my @IN = 1 .. $ARGV[0]; my $COLS = $ARGV[1]; my @OUT = (); pa @IN; for my $i (0 .. $COLS-1) { for (my $j = $i; $j<=$#IN; $j+=$COLS) { push @OUT, $IN[$j] } push @OUT, "{}" } pa @OUT;

Since you are making LaTeX output, each {} in the output marks a column break. Substitute whatever command you need to produce a column break for "{}". This should work for any number of data items and any number of columns.

Update: If you would rather have blank items as placeholders than explicit column breaks, you can change the loop to:

for my $i (0 .. $COLS-1) { push @OUT, @IN[map {$_*$COLS+$i} 0..($#IN/$COLS)]; }

This will leave undef elements in @OUT for each table cell that should be blank. I had initially overlooked that array slices need not be contiguous in Perl and requesting an element off the end of an array produces undef.


In reply to Re: How to warp an array by trading X/Y coordinates (rows/columns) by jcb
in thread How to warp an array by trading X/Y coordinates (rows/columns) by Polyglot

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.