1

I'm trying to transform an array of 3 arrays in an array of 4 where each array is built and ordered with the first element of each other.

I have this:

[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

And I would like the following:

[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]

Any ideas?

2 Answers 2

4

Use Array#transpose.

your_array.transpose

will do it.

Sign up to request clarification or add additional context in comments.

6 Comments

Fastest answer, I have ever seen.. :-)
It is (perhaps) the first time I answered in less than a minute after the post. Usually, it takes two minutes untill I see the question posted.
You wouldn't want to damage your HHKB2. I suggest you let it cool for a few minutes.
@CarySwoveland At this particular moment, my HHKB2 is sleeping, and I am typing on Filco Majestouch.
@ArupRakshit I am more excited about the fact that I am very close to being in the top five Ruby answerers. I was an inactive user for the first year, and overall, it took me four years and three months to reach this position.
|
0
array = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
array.transpose
[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]] 

Is this what you were looking for?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.