Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Appending two arrays horizontally

Reply
Thread Tools

Appending two arrays horizontally

 
 
Edward wijaya
Guest
Posts: n/a
 
      08-07-2004
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
 
Reply With Quote
 
 
 
 
Scott W Gifford
Guest
Posts: n/a
 
      08-07-2004
Edward wijaya <(E-Mail Removed)> writes:

> 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.
 
Reply With Quote
 
 
 
 
Dave Cross
Guest
Posts: n/a
 
      08-07-2004
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...


 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      08-09-2004
On 07 Aug 2004 04:09:22 -0400, Scott W Gifford <(E-Mail Removed)>
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;
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      08-09-2004
On Sat, 07 Aug 2004 16:00:57 +0800, Edward wijaya
<(E-Mail Removed)> wrote:

>@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__
 
Reply With Quote
 
David Combs
Guest
Posts: n/a
 
      08-31-2004
In article <(E-Mail Removed)>,
Michele Dondi <(E-Mail Removed)> wrote:
>On 07 Aug 2004 04:09:22 -0400, Scott W Gifford <(E-Mail Removed)>
>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




 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-31-2004
David Combs <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> In article <(E-Mail Removed)>,
> Michele Dondi <(E-Mail Removed)> wrote:
> >On 07 Aug 2004 04:09:22 -0400, Scott W Gifford <(E-Mail Removed)>
> >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
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      08-31-2004
On Tue, 31 Aug 2004 04:54:29 +0000 (UTC), http://www.velocityreviews.com/forums/(E-Mail Removed) (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"
 
Reply With Quote
 
David Combs
Guest
Posts: n/a
 
      09-02-2004
Thanks to both of you.

I'll give the example a try.

Thanks again!

David

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.43 How do I compute the difference of two arrays? How do I compute the intersection of two arrays? PerlFAQ Server Perl Misc 0 02-02-2011 05:00 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
hash of arrays - appending to one of the arrays Adam Akhtar Ruby 5 03-25-2008 11:53 PM
two arrays problem (although different from the other two arrays) Kev Jackson Ruby 2 03-29-2006 03:58 PM
Two Cells lined horizontally using CSS JSOUL HTML 3 11-09-2005 11:35 PM



Advertisments