combining two arrays

Discussion in 'Perl Misc' started by tgiles@gmail.com, Feb 28, 2005.

  1. Guest

    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.
    , Feb 28, 2005
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Feb 28, 2005
    #2
    1. Advertising

  3. Ala Qumsieh Guest

    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
    Ala Qumsieh, Feb 28, 2005
    #3
  4. Also sprach :

    > 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=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
    Tassilo v. Parseval, Mar 1, 2005
    #4
  5. 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
    Josef Moellers, Mar 1, 2005
    #5
  6. Anno Siegel Guest

    <> 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
    Anno Siegel, Mar 1, 2005
    #6
  7. Paul Lalli Guest

    "Jim Gibson" <> wrote in message
    news:280220051620113899%...
    > In article <>,
    > <> 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
    Paul Lalli, Mar 1, 2005
    #7
  8. tgiles Guest

    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
    tgiles, Mar 3, 2005
    #8
  9. Paul Lalli Guest

    "tgiles" <> wrote in message
    news:...
    > 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
    Paul Lalli, Mar 3, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp
    Replies:
    21
    Views:
    1,127
    Philipp
    Jan 20, 2009
  2. Kev Jackson
    Replies:
    2
    Views:
    110
  3. Keith Lee
    Replies:
    8
    Views:
    112
    robic0
    Feb 22, 2006
  4. mike

    Combining object arrays

    mike, Sep 14, 2005, in forum: Javascript
    Replies:
    10
    Views:
    172
  5. mike

    combining 2 multi-dim arrays

    mike, Jan 12, 2006, in forum: Javascript
    Replies:
    3
    Views:
    100
Loading...

Share This Page