Printing multiple Array as multiple column

378 views
Skip to first unread message

Edward Wijaya

unread,
May 11, 2004, 6:18:56 PM5/11/04
to
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


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

John Bokma

unread,
May 11, 2004, 10:37:13 AM5/11/04
to
Edward Wijaya wrote:

> 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?


--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/

Tore Aursand

unread,
May 11, 2004, 10:55:53 AM5/11/04
to
On Tue, 11 May 2004 22:18:56 +0000, Edward Wijaya wrote:
> 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.

--
Tore Aursand <to...@aursand.no>
"The purpose of all war is ultimately peace." (Saint Augustine)

Greg Bacon

unread,
May 11, 2004, 10:55:59 AM5/11/04
to
In article <opr7uv9u...@news.singnet.com.sg>,
Edward Wijaya <ewi...@singnet.com.sg> wrote:

: 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 <slrn7rpg81....@alexandra.delanet.com> for a start in
that direction.

Greg
--
It should be noted that government is never so zealous in suppressing
crime as when that crime consists of direct injury to its own sources of
revenue, as in tax evasion and counterfeiting of its currency.
-- Murray Rothbard

Jamie

unread,
May 11, 2004, 3:59:24 PM5/11/04
to
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.

Richard Morse

unread,
May 11, 2004, 4:16:55 PM5/11/04
to
In article <dc2a9475.04051...@posting.google.com>,
jnu...@hotmail.com (Jamie) wrote:

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

--
Pukku

Anno Siegel

unread,
May 11, 2004, 4:44:38 PM5/11/04
to
Tore Aursand <to...@aursand.no> wrote in comp.lang.perl.misc:

> On Tue, 11 May 2004 22:18:56 +0000, Edward Wijaya wrote:
> > 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.

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

Abigail

unread,
May 11, 2004, 6:36:12 PM5/11/04
to
Edward Wijaya (ewi...@singnet.com.sg) wrote on MMMCMVI September
MCMXCIII in <URL:news:opr7uv9u...@news.singnet.com.sg>:
,, 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


#!/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


Abigail
--
use lib sub {($\) = split /\./ => pop; print $"};
eval "use Just" || eval "use another" || eval "use Perl" || eval "use Hacker";

Brad Baxter

unread,
May 11, 2004, 8:55:29 PM5/11/04
to
On Tue, 11 May 2004, Edward Wijaya wrote:

> 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

Edward Wijaya

unread,
May 12, 2004, 5:19:05 PM5/12/04
to
Hi John,

The array is of the same size.


Regards
Edwawrd


On Tue, 11 May 2004 09:37:13 -0500, John Bokma
<postm...@castleamber.com> wrote:

> Edward Wijaya wrote:
>
>> 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?
>
>

--

Reply all
Reply to author
Forward
0 new messages