![]() |
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; -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl |
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 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 |
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); |
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 |
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 |
Re: combining two arrays
"Jim Gibson" <jgibson@mail.arc.nasa.gov> wrote in message
news:280220051620113899%jgibson@mail.arc.nasa.gov. .. > In article <1109633342.736732.181440@l41g2000cwc.googlegroups .com>, > <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 |
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 |
Re: combining two arrays
"tgiles" <tgiles@gmail.com> wrote in message
news:1109808789.281137.154640@z14g2000cwz.googlegr 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 |
| All times are GMT. The time now is 01:51 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.