Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > combining two arrays

Reply
Thread Tools

combining two arrays

 
 
tgiles@gmail.com
Guest
Posts: n/a
 
      02-28-2005
Hi, all. This should be trivial but for some reason I'm terribly stuck.
I suppose that it's just not clicking in some fundamental way for me.

I am attempting to combine two arrays into a third array:

@one = qw(a-one a-two a-three a-four);
@two = qw(b-one b-two b-three b-four);

I'll save you my code mangling and just move to what my goal of the
output would be...

The output would return something on the order of:

a-one b-one
a-two b-two
a-three b-three
a-four b-four

there will always be a 1:1 correspondence between stuff on the left and
right, so there's no chance of an empty entry in the array. Going to
look into hashes now- perhaps that's what I needed all along.

nevertheless, your input would be appreciated. Thanks.

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am attempting to combine two arrays into a third array:
>
> @one = qw(a-one a-two a-three a-four);
> @two = qw(b-one b-two b-three b-four);


my @three;
push @three, [ $one[$_], $two[$_] ] for 0..$#one;

use Data:umper;
print Dumper \@three;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Ala Qumsieh
Guest
Posts: n/a
 
      02-28-2005
(E-Mail Removed) wrote:

> I am attempting to combine two arrays into a third array:
>
> @one = qw(a-one a-two a-three a-four);
> @two = qw(b-one b-two b-three b-four);
>
> I'll save you my code mangling and just move to what my goal of the
> output would be...
>
> The output would return something on the order of:
>
> a-one b-one
> a-two b-two
> a-three b-three
> a-four b-four
>
> there will always be a 1:1 correspondence between stuff on the left and
> right, so there's no chance of an empty entry in the array. Going to
> look into hashes now- perhaps that's what I needed all along.


Arrays have no notion of correspondence. Hashes do. But hashes destroy
the order of your elements.

If that's not a problem, you can do this:

my %hash;
$hash{$one[$_]} = $two[$_] for 0 .. $#one;

Or, more succinctly using a hash slice:

my %hash;
@hash{@one} = @two;

--Ala
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      03-01-2005
Also sprach (E-Mail Removed):

> Hi, all. This should be trivial but for some reason I'm terribly stuck.
> I suppose that it's just not clicking in some fundamental way for me.
>
> I am attempting to combine two arrays into a third array:
>
> @one = qw(a-one a-two a-three a-four);
> @two = qw(b-one b-two b-three b-four);
>
> I'll save you my code mangling and just move to what my goal of the
> output would be...
>
> The output would return something on the order of:
>
> a-one b-one
> a-two b-two
> a-three b-three
> a-four b-four
>
> there will always be a 1:1 correspondence between stuff on the left and
> right, so there's no chance of an empty entry in the array. Going to
> look into hashes now- perhaps that's what I needed all along.


A hash could be used, but you'll lose the ordering of the elements. You
can use List::MoreUtils' zip() to combine any number (up to 32 actually)
arrays into one:

use List::MoreUtils qw/zip/;

my @tree = zip @one, @two;

This results in one flat list. If the elements of @one are unique, you
could create a hash just as easily:

my %three = zip @one, @two;

Tassilo
--
use bigint;
$n=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      03-01-2005
(E-Mail Removed) wrote:
> Hi, all. This should be trivial but for some reason I'm terribly stuck.
> I suppose that it's just not clicking in some fundamental way for me.
>
> I am attempting to combine two arrays into a third array:
>
> @one = qw(a-one a-two a-three a-four);
> @two = qw(b-one b-two b-three b-four);
>
> I'll save you my code mangling and just move to what my goal of the
> output would be...
>
> The output would return something on the order of:
>
> a-one b-one
> a-two b-two
> a-three b-three
> a-four b-four
>
> there will always be a 1:1 correspondence between stuff on the left and
> right, so there's no chance of an empty entry in the array. Going to
> look into hashes now- perhaps that's what I needed all along.
>
> nevertheless, your input would be appreciated. Thanks.


TMTOWTDI

foreach (@one) {
print $_, " ", shift(@two), "\n";
}

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-01-2005
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi, all. This should be trivial but for some reason I'm terribly stuck.
> I suppose that it's just not clicking in some fundamental way for me.
>
> I am attempting to combine two arrays into a third array:
>
> @one = qw(a-one a-two a-three a-four);
> @two = qw(b-one b-two b-three b-four);
>
> I'll save you my code mangling and just move to what my goal of the
> output would be...
>
> The output would return something on the order of:
>
> a-one b-one
> a-two b-two
> a-three b-three
> a-four b-four
>
> there will always be a 1:1 correspondence between stuff on the left and
> right, so there's no chance of an empty entry in the array. Going to
> look into hashes now- perhaps that's what I needed all along.


my @combo = ( @one, @two)[ map { $_, @one + $_ } 0 .. $#one];

Anno
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      03-01-2005
"Jim Gibson" <(E-Mail Removed)> wrote in message
news:280220051620113899%(E-Mail Removed). ..
> In article <(E-Mail Removed) .com>,
> <(E-Mail Removed)> wrote:
>
> > Hi, all. This should be trivial but for some reason I'm terribly

stuck.
> > I suppose that it's just not clicking in some fundamental way for

me.
> >
> > I am attempting to combine two arrays into a third array:
> >
> > @one = qw(a-one a-two a-three a-four);
> > @two = qw(b-one b-two b-three b-four);
> >
> > I'll save you my code mangling and just move to what my goal of the
> > output would be...
> >
> > The output would return something on the order of:
> >
> > a-one b-one
> > a-two b-two
> > a-three b-three
> > a-four b-four
> >
> > there will always be a 1:1 correspondence between stuff on the left

and
> > right, so there's no chance of an empty entry in the array. Going to
> > look into hashes now- perhaps that's what I needed all along.

>
> A good candidate for C-style loops, assuming you really want string
> concatenation with one space inserted between strings (untested):
>
> my @three;
> for( my $i = 0; $i < @one; $i++ ) {
> $three[$i] = "$one[$i] $two[$i]";
> }


There's no reason to use C-Style for loops here:

for (0..$#one){
push @three, "$one[$_] $two[$_]";
}

or more perl-ish...

my @three = map "$one[$_] $two[$_]", 0..$#one;

Paul Lalli

 
Reply With Quote
 
tgiles
Guest
Posts: n/a
 
      03-03-2005
Forgot to take the time to update this. Please forgive me.

Looks like Mr. Gibson struck gold. Was able to use the code snippet to
finish up my little script and it runs like a dream. Mr. Lalli, that
looks pretty intriguing as well. I might plug it in and see where it
gets me. I appreciate your input as well!

Anyway, thanks so much for the assist, guys. Slugged it for three days
and it just didn't seem to click. One post and I went from zero to
finished.

Thanks to all the responses. Here's hoping that it will help someone
else down the line.

Cheers!

tom

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      03-03-2005
"tgiles" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Forgot to take the time to update this. Please forgive me.
>
> Looks like Mr. Gibson struck gold. Was able to use the code snippet to
> finish up my little script and it runs like a dream. Mr. Lalli, that
> looks pretty intriguing as well. I might plug it in and see where it
> gets me. I appreciate your input as well!


I appreciate your appreciation. I would appreciate it far more if you
would quote some context in your post that tells me what the heck you're
appreciating.

Have you read the Posting Guidelines that are sent to this group twice a
week?

Glad to be of service, whatever it was.

Paul Lalli

 
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
Merging two arrays -> array of arrays Allen Walker Ruby 6 05-21-2010 08:31 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Two arrays: return only values that exist in both arrays? Joshua Muheim Ruby 8 11-23-2007 07:04 PM
two arrays problem (although different from the other two arrays) Kev Jackson Ruby 2 03-29-2006 03:58 PM



Advertisments