combining two arrays

T

tgiles

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

Gunnar Hjalmarsson

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::Dumper;
print Dumper \@three;
 
A

Ala Qumsieh

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
 
T

Tassilo v. Parseval

Also sprach (e-mail address 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
 
J

Josef Moellers

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";
}
 
A

Anno Siegel

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
 
P

Paul Lalli

Jim Gibson said:
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
 
T

tgiles

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
 
P

Paul Lalli

tgiles said:
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
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top