2 dimensional array to single arrays

J

John Butler

Hi,

I have a nasty loop which creates 2 single arrays from a 2 dimensional
array,

is there a quicker way to do it?

[["a",1],["b",2],["c",3]]

So i want an array
["a","b","c"]

and

[1,2,3]

Any quick method to do this?

JB
 
M

Michael Tomer

John said:
Hi,

I have a nasty loop which creates 2 single arrays from a 2 dimensional
array,

is there a quicker way to do it?

[["a",1],["b",2],["c",3]]

So i want an array
["a","b","c"]

and

[1,2,3]

Any quick method to do this?

JB

my_array = [["a",1],["b",2],["c",3]]

letters = my_array.collect {|array| array[0]}
numbers = my_array.collect {|array| array[1]}
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top