user1718712
user1718712

Reputation: 887

Array to hash of arrays

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?

Upvotes: 1

Views: 101

Answers (1)

sawa
sawa

Reputation: 168081

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

Hash[arr.each_slice(5).map{|k, *v| [k, v]}]

Upvotes: 6

Related Questions