Merging two arrays -> array of arrays

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

Hi!

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

Bey!

···

--
jugyo

On Fri, May 21, 2010 at 1:47 PM, Allen Walker <auswalk@gmail.com> 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/\.

Maybe like this:

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

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

···

-----Original Message-----
From: auswalk@gmail.com [mailto:auswalk@gmail.com]
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/.

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

Jesus.

···

On Fri, May 21, 2010 at 6:47 AM, Allen Walker <auswalk@gmail.com> 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.

Awesome. Thanks

···

--
Posted via http://www.ruby-forum.com/.

I was typoing...

Bey! => Bye!

···

On Fri, May 21, 2010 at 1:53 PM, kohno kazuyuki <kkohno@gmail.com> wrote:

Hi!

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

Bey!

--
jugyo

On Fri, May 21, 2010 at 1:47 PM, Allen Walker <auswalk@gmail.com> 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/\.

Didn't see kohno's answer

···

-----Original Message-----
From: Reinhart Viane [mailto:rv@domos.be]
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[i],b[i]]
  i+=1
end

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

-----Original Message-----
From: auswalk@gmail.com [mailto:auswalk@gmail.com]
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/.