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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |