ruby array of array with repeated values to hash of hash -


I'm new to Ruby and I find it hard to figure out how to convert array array into a hash For example, I have:

  [[38, "s", "we"], [38, "t", ", [45," s "," we [45] [52] "T", "[45]", [45] "T", "Ram"], [52, "s", "we"], [38, "t", "bar"], [45] I want to finally:  
  {38 =>   

"S" => ["we"], "t" => [["foo", "bar"]}, 45 => {"S" => ["we"], "T" => ["Rama"]}, 52 => {"S" => ["we"], "t" => ["cat", " I have tried the group and the hush, but I am not giving it to me what I am looking for.

< Perhaps there is a more concise way to do this, but I decided to go straight to the path: <[38, "T", "Foo"], div class = "post-text" itemprop = "text" [52, "T", "cat"], [52, "s", "we", [38] "[45]" [52] ], [45, "T", "Rama"], [52, T "," dog "]] output = {} # I will talk through the first walk in the comment Input.each do | Outer_key, inner_key, value | # Set output [38] has a new hash because production [38] is not set yet. # If this was already determined, then this line would not do anything, so # output [38] will retain its previous data output [outer_key] || = {} # Set output [38] ["s"] in a new array, because the output [38] ["s"] is not set yet # if it was already set, Will also not, so the # output [38] ["s"] will retain its previous data production [external_key] [internal_key] || = [] # Add "we" to the array in the output [38] ["s"] Output [external_key] [internal_key] & lt; & Lt; The end of the value

Therefore, the part you really use, all have been merged:

  output = {} input. External_key, interior_key, value | Output [external_key] || = {} Output [external_key] [internal_key] || = [] Output [external_key] [internal_key] & lt; & Lt; Value expiration    

Comments