Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Printing multiple arrays as multiple column

by monkfan (Curate)
on Apr 12, 2005 at 03:18 UTC ( #446821=snippet: print w/ replies, xml ) Need Help??

Description: This is not my invention but abigail's.
It was an answer to my question sometime ago.
I thought, the snippet was too cool to be missed out.
#!/usr/bin/perl 

use strict; 
use warnings; 
no warnings qw /syntax/; 

sub zup { 
    join "\n" => map {join " " => map {shift @$_} @_} @{$_ [0]} 
} 


my @array1 = qw /ab bc cd de/; 
my @array2 = qw /cc dd ee gg/; 
my @array3 = qw /12 34 56 78/; 

print zup \(@array1, @array2, @array3); 
print "\n"; 

__END__ 
ab cc 12 
bc dd 34 
cd ee 56 
de gg 78
Comment on Printing multiple arrays as multiple column
Download Code
Re: Printing multiple arrays as multiple column
by tlm (Prior) on Apr 12, 2005 at 04:16 UTC

    Sheer wizardry! Thanks for sharing it.

    I should point out that zup is destructive, as you can see if you try:

    print zup \(@array1, @array2, @array3); print "\n"; print zup \(@array1, @array2, @array3); print "\n"; __END__ ab cc 12 bc dd 34 cd ee 56 de gg 78
    Also, I don't think the line
    no warnings qw/syntax/;
    is necessary.

    the lowliest monk

Re: Printing multiple arrays as multiple column
by tlm (Prior) on Apr 12, 2005 at 04:47 UTC

    Here's a non-destructive version of zup:

    #!/usr/bin/perl use strict; use warnings; sub zup { join "\n", map { my $i = $_; join ' ', map $_->[ $i ], @_ } 0 .. $#{ + $_[0] } } my @array1 = qw /ab bc cd de/; my @array2 = qw /cc dd ee gg/; my @array3 = qw /12 34 56 78/; print zup \(@array1, @array2, @array3); print "\n"; print zup \(@array1, @array2, @array3); print "\n"; __END__ ab cc 12 bc dd 34 cd ee 56 de gg 78 ab cc 12 bc dd 34 cd ee 56 de gg 78

    the lowliest monk

Re: Printing multiple arrays as multiple column
by gopalr (Priest) on Apr 12, 2005 at 05:28 UTC

    Hi,

    Also List::MoreUtils module done this job

    Example

    use strict; use List::MoreUtils qw(:all); my @array1 = qw /ab bc cd de/; my @array2 = qw /cc dd ee gg/; my @array3 = qw /12 34 56 78/; my $result = each_array(@array1, @array2, @array3); while ( my ($a, $b, $c) = $result->() ) { print "\n$a $b $c"; }

    Output

    ab cc 12 bc dd 34 cd ee 56 de gg 78

    Thanks

    Gopal.R

      I'ts always seemed to me regrettable that List::MoreUtils chose to take the iterator approach to this. I find the transpose function a lot more useful:

      sub transpose { map { my $i = $_; [ map $_->[ $i ], @_ ] } 0 .. $#{ $_[0] } } my @array1 = qw /ab bc cd de/; my @array2 = qw /cc dd ee gg/; my @array3 = qw /12 34 56 78/; print "@$_\n" for transpose \( @array1, @array2, @array3 ); __END__ ab cc 12 bc dd 34 cd ee 56 de gg 78
      As shown above, iterating throught the transpose is as easy as iterating through any other array, and much more flexible (e.g. one can easily iterate through the rows in reverse order, etc.).

      One handy use for the transpose is to feed multi-arg functions via map; e.g.:

      my @firsts = ( 1, 2, 3, 4 ); my @seconds = qw( w x y z ); my @thirds = ( 5.6, 7.8, 9.0, 1.2 ); my @foos = map foo( @$_ ), transpose \( @firsts, @seconds, @thirds ); # @foos gets foo( 1, 'w', 5.6 ), foo( 2, 'x', 7.8 ), etc.
      Plus, the transpose of a numerical matrix is a mathematically useful entity of its own right (though admittedly, one wouldn't use Perl AoA's for serious matrix-oriented computation, but rather something like PDL.)

      the lowliest monk

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://446821]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2014-09-20 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (153 votes), past polls