Merging two arrays -> array of arrays

Discussion in 'Ruby' started by Allen Walker, May 21, 2010.

  1. Allen Walker

    Allen Walker Guest

    Example:

    a = [1,2,3]
    b = [4,5,6]

    I want to merge them so the new array is:

    [ [1,4], [2,5], [3,6] ]

    Can't figure out the best way to do this.

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
    Allen Walker, May 21, 2010
    #1
    1. Advertising

  2. Hi!

    [a, b].transpose
    => [[1, 4], [2, 5], [3, 6]]

    Bey!

    --
    jugyo


    On Fri, May 21, 2010 at 1:47 PM, Allen Walker <> wrote:
    > Example:
    >
    > a = [1,2,3]
    > b = [4,5,6]
    >
    > I want to merge them so the new array is:
    >
    > [ [1,4], [2,5], [3,6] ]
    >
    > Can't figure out the best way to do this.
    >
    > Thanks
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    kohno kazuyuki, May 21, 2010
    #2
    1. Advertising

  3. Allen Walker

    Allen Walker Guest

    Allen Walker, May 21, 2010
    #3
  4. I was typoing...

    Bey! => Bye!

    On Fri, May 21, 2010 at 1:53 PM, kohno kazuyuki <> wrote:
    > Hi!
    >
    > [a, b].transpose
    > => [[1, 4], [2, 5], [3, 6]]
    >
    > Bey!
    >
    > --
    > jugyo
    >
    >
    > On Fri, May 21, 2010 at 1:47 PM, Allen Walker <> wrote:
    >> Example:
    >>
    >> a = [1,2,3]
    >> b = [4,5,6]
    >>
    >> I want to merge them so the new array is:
    >>
    >> [ [1,4], [2,5], [3,6] ]
    >>
    >> Can't figure out the best way to do this.
    >>
    >> Thanks
    >> --
    >> Posted via http://www.ruby-forum.com/.
    >>
    >>

    >
    >
    kohno kazuyuki, May 21, 2010
    #4
  5. Re: [SPAM] Merging two arrays -> array of arrays

    Maybe like this:

    i=0
    a = [1,2,3]
    b = [4,5,6]
    new_array=[]
    while i<a.length
    new_array<<[a,b]
    i+=1
    end

    untested and it assumes that length of the array a and b are always the same

    -----Original Message-----
    From: [mailto:]
    Sent: vrijdag 21 mei 2010 6:47
    To: ruby-talk ML
    Subject: [SPAM] Merging two arrays -> array of arrays
    Importance: Low

    Example:

    a = [1,2,3]
    b = [4,5,6]

    I want to merge them so the new array is:

    [ [1,4], [2,5], [3,6] ]

    Can't figure out the best way to do this.

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
    Reinhart Viane, May 21, 2010
    #5
  6. Re: [SPAM] Merging two arrays -> array of arrays

    Didn't see kohno's answer

    -----Original Message-----
    From: Reinhart Viane [mailto:]
    Sent: vrijdag 21 mei 2010 9:32
    To: ruby-talk ML
    Subject: Re: [SPAM] Merging two arrays -> array of arrays

    Maybe like this:

    i=0
    a = [1,2,3]
    b = [4,5,6]
    new_array=[]
    while i<a.length
    new_array<<[a,b]
    i+=1
    end

    untested and it assumes that length of the array a and b are always the same

    -----Original Message-----
    From: [mailto:]
    Sent: vrijdag 21 mei 2010 6:47
    To: ruby-talk ML
    Subject: [SPAM] Merging two arrays -> array of arrays
    Importance: Low

    Example:

    a = [1,2,3]
    b = [4,5,6]

    I want to merge them so the new array is:

    [ [1,4], [2,5], [3,6] ]

    Can't figure out the best way to do this.

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
    Reinhart Viane, May 21, 2010
    #6
  7. On Fri, May 21, 2010 at 6:47 AM, Allen Walker <> wrote:
    > Example:
    >
    > a = [1,2,3]
    > b = [4,5,6]
    >
    > I want to merge them so the new array is:
    >
    > [ [1,4], [2,5], [3,6] ]
    >
    > Can't figure out the best way to do this.


    irb(main):001:0> [1,2,3].zip([4,5,6])
    => [[1, 4], [2, 5], [3, 6]]

    Jesus.
    Jesús Gabriel y Galán, May 21, 2010
    #7
    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. JNU
    Replies:
    3
    Views:
    357
    Roedy Green
    Sep 3, 2003
  2. merging two doms

    , Jul 13, 2005, in forum: Java
    Replies:
    1
    Views:
    420
    Alan Krueger
    Jul 13, 2005
  3. Mark
    Replies:
    3
    Views:
    688
    Oliver Wong
    Mar 31, 2006
  4. Kev Jackson
    Replies:
    2
    Views:
    110
  5. Chris Chris

    Merging two arrays

    Chris Chris, Sep 22, 2008, in forum: Ruby
    Replies:
    4
    Views:
    140
    Henning Bekel
    Sep 22, 2008
Loading...

Share This Page