Polyglot has asked for the wisdom of the Perl Monks concerning the following question:
I'm pushing a large number of items into an array that is later converted into columns in LaTeX. LaTeX puts the items in order, vertically, spilling over into the next column after each column is filled. I need the items to be placed across the page in rows, instead of vertically. Owing to code that allows the column count to be adjustable, I cannot predict how many columns there will be, but I can use the user-variable for the column count to help transform the array. It seems like something like 'map' should be able to do this efficiently, but I have no idea how.
I could do this by brute force, of course, creating a separate array for each column, then shifting off the original array and shuffling to each column's array, then pushing them all into a single array again. But that just doesn't seem like the Perl way to do things. I couldn't find anything about this here or via Google--but it seems someone might have wanted to transform an array like this before. Your wisdom is welcome.
Essentially, I would like to convert something like this:
One | Five | Nine | Thirteen |
Two | Six | Ten | Fourteen |
Three | Seven | Eleven | Fifteen |
Four | Eight | Twelve | Sixteen |
Into something like this:
One | Two | Three | Four |
Five | Six | Seven | Eight |
Nine | Ten | Eleven | Twelve |
Thirteen | Fourteen | Fifteen | Sixteen |
Look like fun?
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by choroba (Cardinal) on Mar 08, 2016 at 15:51 UTC
|
Note that for squares (as in your example), you can rotate the table in place:
#!/usr/bin/perl
use warnings;
use strict;
use Text::Table;
my @tab = ( [qw[ One Five Nine Thirteen ]],
[qw[ Two Six Ten Fourteen ]],
[qw[ Three Seven Eleven Fifteen ]],
[qw[ Four Eight Twelve Sixteen ]],
);
print 'Text::Table'->new(@tab);
for my $i (0 .. $#tab) {
for my $j (0 .. $i - 1) {
( $tab[$i][$j], $tab[$j][$i] ) = ( $tab[$j][$i], $tab[$i][$j]
+);
}
}
print '-' x 20, "\n";
print 'Text::Table'->new(@tab);
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord
}map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
| [reply] [d/l] [select] |
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by BrowserUk (Patriarch) on Mar 08, 2016 at 15:39 UTC
|
#! perl -slw
use strict;
use Data::Dump qw[ pp ];
my @data = map[ split ' ' ], <DATA>;
pp \@data;
my @xformed;
push @xformed, [ map shift @$_ // (), @data ] while @{ $data[ 0 ] };
pp \@xformed;
__DATA__
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Output: C:\test>1157088.pl
[[1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
Wow, you both are fast! Thank you! I thought there must be a way to do this with map. I'm not working on single characters, but with split I think I can find a suitable delimiter. (In my case, I'm actually working with LaTeX codes for each entry, so each array "element" is actually a piece of code text.) I'll give your suggestion a try.
| [reply] |
|
C:\test>1157088.pl
[
["one", "five", "nine", "thirteen"],
["two", "six", "ten", "fourteen"],
["three", "seven", "eleven", "fifteen"],
["four", "eight", "twelve", "sixteen"],
]
[
["one", "two", "three", "four"],
["five", "six", "seven", "eight"],
["nine", "ten", "eleven", "twelve"],
["thirteen", "fourteen", "fifteen", "sixteen"],
]
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by toolic (Bishop) on Mar 08, 2016 at 15:35 UTC
|
| [reply] |
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by VinsWorldcom (Prior) on Mar 08, 2016 at 17:20 UTC
|
| [reply] |
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by Your Mother (Archbishop) on Mar 08, 2016 at 17:27 UTC
|
| [reply] |
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by jcb (Parson) on Mar 09, 2016 at 00:44 UTC
|
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. | [reply] [d/l] [select] |
|
|