Appending two arrays horizontally

E

Edward wijaya

Hi,

I have this two arrays:

@arr1 = qw(3 2 2 1);
@arr2 = qw(cc dd ff gg);

is there any way I can append this two arrays
so that it becomes

@arr3 = ["3 cc", "2 dd", "3 ff", "1 gg"];

Thanks before hand

Regards
Edward WIJAYA
SINGAPORE
 
S

Scott W Gifford

Edward wijaya said:
Hi,

I have this two arrays:

@arr1 = qw(3 2 2 1);
@arr2 = qw(cc dd ff gg);

is there any way I can append this two arrays
so that it becomes

@arr3 = ["3 cc", "2 dd", "3 ff", "1 gg"];

Assuming you know in advance they're both the same size:

my @arr3 = map { join(" ",$arr1[$_],$arr2[$_]) } (0..$#arr1);

This loops from 0 to the last element in @arr1, and for each number
joins the appropriate array elements together with a space.

----ScottG.
 
D

Dave Cross

Hi,

I have this two arrays:

@arr1 = qw(3 2 2 1);
@arr2 = qw(cc dd ff gg);

is there any way I can append this two arrays
so that it becomes

@arr3 = ["3 cc", "2 dd", "3 ff", "1 gg"];

@arr3 = map { "$arr1[$_] $arr2[$_}" } 0 .. $#arr1

This assumes that you know the arrays are of equal length.

Dave...
 
M

Michele Dondi

my @arr3 = map { join(" ",$arr1[$_],$arr2[$_]) } (0..$#arr1);

Well, I'm not a big fan of useless quoting, but

my @arr3 = map "$arr1[$_],$arr2[$_]", 0..$#arr1;

is more clear IMHO.
Assuming you know in advance they're both the same size:

Also, not difficult to cope with this:

my @arr3 = do {
no warnings 'uninitialized';
map "$arr1[$_] $arr2[$_]",
0 .. ($#arr1>$#arr2 ? $#arr1 : $#arr2);
};

(implicitly assuming C<use warnings;> as it "Should"!)


PS: anser if you like, but then take into account that most probably I
won't be able to read news for one or two weeks or possibly even till
September!


Michele
 
M

Michele Dondi

@arr1 = qw(3 2 2 1);
@arr2 = qw(cc dd ff gg);

is there any way I can append this two arrays
so that it becomes

@arr3 = ["3 cc", "2 dd", "3 ff", "1 gg"];

Isn't the C<zip> operator supposed to handle exactly this kind of
situations? Oh, but were you still talking Perl5?!? ;-)


PS: anser if you like, but then take into account that most probably I
won't be able to read news for one or two weeks or possibly even till
September!


Michele
 
D

David Combs

my @arr3 = map { join(" ",$arr1[$_],$arr2[$_]) } (0..$#arr1);

Well, I'm not a big fan of useless quoting, but

my @arr3 = map "$arr1[$_],$arr2[$_]", 0..$#arr1;

is more clear IMHO.
Assuming you know in advance they're both the same size:

Also, not difficult to cope with this:

my @arr3 = do {
no warnings 'uninitialized';
map "$arr1[$_] $arr2[$_]",
0 .. ($#arr1>$#arr2 ? $#arr1 : $#arr2);
};

(implicitly assuming C<use warnings;> as it "Should"!)

Why the no-warnings stmt?

In case one of the arrays is uninitialized?

Or in case one of the values *within* one of the
arrays is?



Maybe I've been blind, but this is the first
time I've seen a no-warnings used in this kind
of situation. (Well, at least it's rarely seen
in this group's code-pieces.)

Anything to say about whether to do this
in general?

Also -- *why* turn off the warnings, ie why
don't you *want* to know about them (these "errors"?)?

Thanks!

David
 
A

Anno Siegel

David Combs said:
my @arr3 = map { join(" ",$arr1[$_],$arr2[$_]) } (0..$#arr1);

Well, I'm not a big fan of useless quoting, but

my @arr3 = map "$arr1[$_],$arr2[$_]", 0..$#arr1;

is more clear IMHO.
Assuming you know in advance they're both the same size:

Also, not difficult to cope with this:

my @arr3 = do {
no warnings 'uninitialized';
map "$arr1[$_] $arr2[$_]",
0 .. ($#arr1>$#arr2 ? $#arr1 : $#arr2);
};

(implicitly assuming C<use warnings;> as it "Should"!)

Why the no-warnings stmt?

In case one of the arrays is uninitialized?

No. An uninitialized array is never a problem, it behaves like an
empty array. The same goes for hashes, it's only scalars where
the distinction makes a difference.
Or in case one of the values *within* one of the
arrays is?

Possibly that, but mainly to catch undefined values that come from
*outside* one of the arrays.

The arrays will in general have different lengths and the index runs
over the longer of the two. So the shorter array will be probed beyond
its end and return undefs. The main purpose of "no warnings ..." is
to catch those.

Anno
 
M

Michele Dondi

my @arr3 = do {
no warnings 'uninitialized';
map "$arr1[$_] $arr2[$_]",
0 .. ($#arr1>$#arr2 ? $#arr1 : $#arr2);
};

(implicitly assuming C<use warnings;> as it "Should"!)

Why the no-warnings stmt?

Not to get (one of those rare authentically) *unwanted* warnings.
In case one of the arrays is uninitialized?

Or in case one of the values *within* one of the
arrays is?

The latter, with the former as a special case of it.
Anything to say about whether to do this
in general?

In general there are situations in which it is desirable or even
advisable to *locally* disable some warnings or strictures.
Also -- *why* turn off the warnings, ie why
don't you *want* to know about them (these "errors"?)?

Because I *do* know in advance that I may get a warning that is not
really something going wrong.

In (my) practice C<no warnings 'uninitialized'> is the most frequently
used and the most reasonable one.

Hope this example clarifies things up (I moved the code to a sub):


#!/usr/bin/perl -l

use strict;
use warnings;

sub zip (\@\@) {
my @a=@{ $_[0] };
my @b=@{ $_[1] };
# no warnings 'uninitialized';
map "$a[$_] $b[$_]",
0 .. ($#a>$#b ? $#a : $#b);
}

print '@arr1 and @arr2 have the same length';
my @arr1 = qw /foo bar baz/;
my @arr2 = 0 .. $#arr1;
print for (zip @arr1, @arr2), '';

print '@arr1 and @arr2 have different lengths';
@arr2 = 0 .. @arr1;
print for zip @arr1, @arr2;

__END__


If you run it, then you get:


# ./foo.pl
@arr1 and @arr2 have the same length
foo 0
bar 1
baz 2

@arr1 and @arr2 have different lengths
Use of uninitialized value in concatenation (.) or string at
../foo.pl line 10.
foo 0
bar 1
baz 2
3


If you uncomment the <no warnings> line the warning goes away and you
get the (supposedly) desired output. Of course it is implicit that I
made an educated guess at what the desired output is for undefined
entries...


Michele
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top