2 dimensional array to single arrays

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

2008/11/21 John B. [email protected]:

Any quick method to do this?

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

Farrel

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs