2 dimensional array to single arrays


#1

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


#2

2008/11/21 John B. removed_email_address@domain.invalid:

Any quick method to do this?

letters, numbers = [“a”,1],[“b”,2],[“c”,3]].transpose

Farrel


#3

John B. wrote:

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]}