# Appending two arrays horizontally

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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

## Re: Appending two arrays horizontally

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

## Re: Appending two arrays horizontally

wrote:

>    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
--
\$\=q.,.,\$_=q.print'  ,\g,,( w,a'c'e'h,,map{\$_-=qif/g/;chr
}107..q[..117,q)[map+hex,split//,join' ,2B,, w\$ECDF078D3'
F9'5F3014\$,\$,];];\$\.=\$/,s,q,32,g,s,g,112,g,y,' , q,,eval;

## Re: Appending two arrays horizontally

>wrote:
>
>>    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

## Re: Appending two arrays horizontally

> >wrote:
> >
> >>    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

## Re: Appending two arrays horizontally

On Tue, 31 Aug 2004 04:54:29 +0000 (UTC), dkcombs@panix.com (David
Combs) wrote:

>>  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
--
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
"perl bug File::Basename and Perl's nature"

## Re: Appending two arrays horizontally

Thanks to both of you.

I'll give the example a try.

Thanks again!

David

## Re: Appending two arrays horizontally

On Sat, 07 Aug 2004 16:00:57 +0800, Edward wijaya wrote:

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

## Re: Appending two arrays horizontally

On Sat, 07 Aug 2004 16:00:57 +0800, Edward wijaya

>@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
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and  print   q... <DATA>;
__END__