KonstantinP
KonstantinP

Reputation: 27

Flatten hash with two identical key names(array)

I like method shown here - https://stackoverflow.com/a/9648410/1646893 and I want to use it in my case. What if we have something like this:

{"a"=>{"b"=>"111", "c"=>"9", "d"=>{"ff"=>{"uu"=>[{"q"=>"77", "r"=>{"w"=>"66"}, "j"=>{"@l"=>"44"}}, {"q"=>"78", "r"=>{"w"=>"67"}, "j"=>{"@l"=>"45"}}]}, "@e"=>"56"}, "@b1"=>"01", "@b2"=>"02", "@b3"=>"03"}}

And When I tried to use our method:

{["a", "b"]=>"111", ["a", "c"]=>"9", ["a", "d", "ff", "uu"]=>[{"q"=>"77", "r"=>{"w"=>"66"}, "j"=>{"@l"=>"44"}}, {"q"=>"78", "r"=>{"w"=>"67"}, "j"=>{"@l"=>"45"}}], ["a", "d", "@e"]=>"56", ["a", "@b1"]=>"01", ["a", "@b2"]=>"02", ["a", "@b3"]=>"03"}

The result was an array with 2 values:

[{"q"=>"77", "r"=>{"w"=>"66"}, "j"=>{"@l"=>"44"}}, {"q"=>"78", "r"=>{"w"=>"66"}, "j"=>{"@l"=>"44"}}]

This is what i want to get:

["a", "d", "ff", "uu", "q0", "w0", "j0", "@l0"]=>"44"

etc.

Maybe I should change key names before using flat_hash method? What should i do to have the result without an array and key names contain?

Upvotes: 0

Views: 170

Answers (1)

Phrogz
Phrogz

Reputation: 303251

Here's my answer that produces what I believe is a better output that what you are asking for. If you absolutely desperately need exactly the output you have asked for, say so and perhaps I'll provide a variation that produces it.

class Hash;  def each_with_key; each{ |k,v|            yield(v,k) }; end; end
class Array; def each_with_key; each.with_index{ |v,i| yield(v,i) }; end; end

def path_to_values(hash)
  {}.tap do |result|
    crawl = ->(o,chain=[]) do
      o.each_with_key do |v,k|
        path = chain + [k]
        (v.is_a?(Hash) || v.is_a?(Array)) ? crawl[v,path] : result[path] = v
      end
    end
    crawl[hash]
  end
end

Seen in action:

h = {
  "a" => {
    "b"=>"111", "c"=>"9",
    "d"=>{
      "ff"=>{
        "uu"=>[
          { "q"=>"77",
            "r"=>{"w"=>"66"},
            "j"=>{"@l"=>"44"}},
          { "q"=>"78",
            "r"=>{"w"=>"67"},
            "j"=>{"@l"=>"45"}}
        ]
      },
      "@e"=>"56"
    },
    "@b1"=>"01", "@b2"=>"02", "@b3"=>"03"
  }
}
require 'pp'
pp path_to_values(h)
#=> {["a", "b"]=>"111",
#=>  ["a", "c"]=>"9",
#=>  ["a", "d", "ff", "uu", 0, "q"]=>"77",
#=>  ["a", "d", "ff", "uu", 0, "r", "w"]=>"66",
#=>  ["a", "d", "ff", "uu", 0, "j", "@l"]=>"44",
#=>  ["a", "d", "ff", "uu", 1, "q"]=>"78",
#=>  ["a", "d", "ff", "uu", 1, "r", "w"]=>"67",
#=>  ["a", "d", "ff", "uu", 1, "j", "@l"]=>"45",
#=>  ["a", "d", "@e"]=>"56",
#=>  ["a", "@b1"]=>"01",
#=>  ["a", "@b2"]=>"02",
#=>  ["a", "@b3"]=>"03"}

Upvotes: 1

Related Questions