Merging two arrays -> array of arrays

A

Allen Walker

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
 
K

kohno kazuyuki

I was typoing...

Bey! => Bye!

Hi!

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

Bey!

--
jugyo


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
 
R

Reinhart Viane

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: (e-mail address removed) [mailto:[email protected]]
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
 
R

Reinhart Viane

Didn't see kohno's answer

-----Original Message-----
From: Reinhart Viane [mailto:[email protected]]
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: (e-mail address removed) [mailto:[email protected]]
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
 
J

Jesús Gabriel y Galán

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top