Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   combining two arrays (http://www.velocityreviews.com/forums/t891094-combining-two-arrays.html)

 tgiles@gmail.com 02-28-2005 11:29 PM

combining two arrays

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.

Re: combining two arrays

tgiles@gmail.com 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::Dumper;
print Dumper \@three;

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 Ala Qumsieh 02-28-2005 11:46 PM

Re: combining two arrays

tgiles@gmail.com 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

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

 Tassilo v. Parseval 03-01-2005 05:52 AM

Re: combining two arrays

Also sprach tgiles@gmail.com:

> 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+=8)<=200);

 Josef Moellers 03-01-2005 07:49 AM

Re: combining two arrays

tgiles@gmail.com 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

 Anno Siegel 03-01-2005 10:06 AM

Re: combining two arrays

<tgiles@gmail.com> 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

 Paul Lalli 03-01-2005 02:29 PM

Re: combining two arrays

"Jim Gibson" <jgibson@mail.arc.nasa.gov> wrote in message
news:280220051620113899%jgibson@mail.arc.nasa.gov. ..
> <tgiles@gmail.com> 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

 tgiles 03-03-2005 12:13 AM

Re: combining two arrays

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

 Paul Lalli 03-03-2005 06:17 PM

Re: combining two arrays

"tgiles" <tgiles@gmail.com> wrote in message
> 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

 All times are GMT. The time now is 04:38 AM.