1

I have an array of values I got from a table:

arr = ["One", "0", "31.948", "0", "6.94",
       "Two", "0", "31.948", "0", "6.94",
       "Three", "0", "23.961", "0", "5.21"]

I need to get a hash of arrays:

hash = {
  "One" => ["0", "31.948", "0", "6.94"],
  "Two" => ["0", "31.948", "0", "6.94"],
  "Three" => ["0", "23.961", "0", "5.21"]
}

How do I do it?

1
  • 1
    What you have in the expected output is not a valid Ruby object. Commented Oct 18, 2012 at 1:33

1 Answer 1

6

Assuming you want an array for each value, this will do it.

Hash[arr.each_slice(5).map{|k, *v| [k, v]}]
Sign up to request clarification or add additional context in comments.

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.