Printing multiple Array as multiple column

J

John Bokma

Edward said:
Hi,

I have again this array:

@array1 = (ab, bc, cd, ...)
@array2 = (cc, dd, ee, ...)

with: print join("\n", @array), "\n";
it does give:

ab
bc
cd

Can we extend it to for multiple array?

so that it gives:

ab cc
bc dd
cd ee


I tried print join ("\n", @array1, @array2), "\n";
But of no result.

that prints first array1, then array2 etc...

you must specify more exactly, what if array1 is smaller / bigger than
array2?
 
T

Tore Aursand

I have again this array:

@array1 = (ab, bc, cd, ...)
@array2 = (cc, dd, ee, ...)

with: print join("\n", @array), "\n";
it does give:

ab
bc
cd

Can we extend it to for multiple array?

No need to; Text::Table alread does it for us.
 
G

Greg Bacon

: I have again this array:
:
: @array1 = (ab, bc, cd, ...)
: @array2 = (cc, dd, ee, ...)
:
: with: print join("\n", @array), "\n";
: it does give:
:
: ab
: bc
: cd
:
: Can we extend it to for multiple array?
:
: so that it gives:
:
: ab cc
: bc dd
: cd ee

This'll be easier with Perl 6, but you can play map games for now:

#! /usr/local/bin/perl

my @array1 = qw( ab bc cd );
my @array2 = qw( cc dd ee );

print map "$_->[0] $_->[1]\n",
map { [ $array1[$_], $array2[$_] ] }
0 .. $#array1;

Yes, it could stand to be much more general. Take a look at Abigail's
code in <[email protected]> for a start in
that direction.

Greg
 
J

Jamie

I had to do something fairly similar to that...except my results were
being formatted with html tags...

@array1 = (ab, bc, cd, ...)
@array2 = (cc, dd, ee, ...)

foreach $element1(@array1) {
$element2 = shift(@array2);
print qq($element1 $element2);
}


....this should work as long as the arrays are the same length.
 
R

Richard Morse

I had to do something fairly similar to that...except my results were
being formatted with html tags...

@array1 = (ab, bc, cd, ...)
@array2 = (cc, dd, ee, ...)

foreach $element1(@array1) {
$element2 = shift(@array2);
print qq($element1 $element2);
}


...this should work as long as the arrays are the same length.

In the interest of TIMTOWTDI:

#!/usr/bin/perl
use strict;
use warnings;

my @a1 = qw/aa bb cc dd/;
my @a2 = qw/ee ff gg hh/;

while (@a1 || @a2) {
print shift(@a1) . ' ' . shift(@a2) . "\n";
}

__END__

If you don't want to get ugly warnings when one of the arrays is shorter
than the other, you can do 'no warnings qw/uninitialized/;'.

Note that this is a destructive procedure -- if you want your arrays
unscathed at the end, you were better not to use shift.

HTH,
Ricky
 
A

Anno Siegel

Tore Aursand said:
No need to; Text::Table alread does it for us.

Only part of it, really. Text::Table is line-oriented, but we are given
columns. There is no simple way to add whole columns to a table, so the
transposition problem that is being discussed in the other branch of the
thread remains.

The best Text::Table can do is:

my $tb = Text::Table->new( '', ''); # two title-less columns
$tb->add( $array1[ $_], $array2[ $_]) for 0 .. $#array1;
print $tb;

but the alignment problem this solves is non-existent with the given
data. 'print "$array1[ $_] $array2[ $_]\n"' would print the same.

Anno
 
E

Edward Wijaya

Hi,

I have again this array:

@array1 = (ab, bc, cd, ...)
@array2 = (cc, dd, ee, ...)

with: print join("\n", @array), "\n";
it does give:

ab
bc
cd

Can we extend it to for multiple array?

so that it gives:

ab cc
bc dd
cd ee


I tried print join ("\n", @array1, @array2), "\n";
But of no result.

Hope to hear from you guys.
Thanks again so much for your answer.

Regards
Edward WIJAYA
SINGAPORE
 
B

Brad Baxter

Hi,

I have again this array:

@array1 = (ab, bc, cd, ...)
@array2 = (cc, dd, ee, ...) [...]

Can we extend it to for multiple array?

so that it gives:

ab cc
bc dd
cd ee

use warnings;
use strict;
use Array::Each;

my @array1 = qw(ab bc cd de );
my @array2 = qw(cc dd ee ff gg hh );
my @array3 = qw(11 22 33 44 55 );

my $set = Array::Each->new(
set => [ \@array1, \@array2, \@array3 ],
undef => '-',
bound => 0,
);

while( my( $a1, $a2, $a3 ) = $set->each ) {
printf "%12s %12s %12s\n", $a1, $a2, $a3;
}

__END__
ab cc 11
bc dd 22
cd ee 33
de ff 44
- gg 55
- hh -


http://search.cpan.org/~bbaxter/Array-Each-0.02/

Regards,

Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top