logen has asked for the wisdom of the Perl Monks concerning the following question:
First, how do I find the biggest string length? Does anybody have any command shorter thenmy (@b) = qw(un deux trois quatre jedan dva tri tcheteri one two three );
Secondly, I want the array printed or displayed in columns as "tight" as possible and _justified left_. I don't manage justifying left with sprintf(). Let's choose 4 columns. Knowing $max, the shortest I manage is:my ($max) = sort {$b <=> $a} map length, @a;
An awful solution because I destroy the array and the aliasing substr($_ . ' ' x $max, 0, $max +1) is horrible. Better solutions are welcome :-) Finally, I want to "transpose" the previous display, or to "shuffle" it. The best I find ismy $col=4; for(@b){ $_ = substr($_ . ' ' x $max, 0, $max +1)} @a = @b; for (;@a;){print splice (@a, 0, $col),"\n"}
Really disgusting. I want prettier code. The code in one slurp:@a = @b; my @c; for (;@a;){ my @b = splice (@a, 0, $col); map {push @{$c[$_]}, $b[$_]; } (0 .. $#b); } map {print @$_,"\n"} @c;
Thank you for looking at this!use strict; use warnings; my (@a) = my (@b) = qw(un deux trois quatre jedan dva tri tcheteri on +e two three ); print "@a\n"; my ($max) = sort {$b <=> $a} map length, @a; print "max=$max\n"; my $col=4; for(@b){ $_ = substr($_ . ' ' x $max, 0, $max +1)} @a = @b; for (;@a;){print splice (@a, 0, $col),"\n"} print "\ntrasnsposed\n\n"; @a = @b; my @c; for (;@a;){ my @b = splice (@a, 0, $col); map {push @{$c[$_]}, $b[$_]; } (0 .. $#b); } map {print @$_,"\n"} @c;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Printing an array columnwise or rowwise
by ikegami (Patriarch) on Jul 18, 2006 at 01:16 UTC | |
|
Re: Printing an array columnwise or rowwise
by jhourcle (Prior) on Jul 18, 2006 at 02:09 UTC | |
|
Re: Printing an array columnwise or rowwise
by graff (Chancellor) on Jul 18, 2006 at 02:09 UTC | |
|
Re: Printing an array columnwise or rowwise
by logen (Novice) on Jul 18, 2006 at 11:20 UTC |