-5

I have a nested hash:

Hash = { "abc" => { "def" => { "count" => 120 } } ,
          "ABC" => {"DEF" => { "COUNT" => 100 } },
          "sample" => {"samplecode" => {"COUNT" => 3 } } }

I want to convert the entire hash into lowercase. If any duplicates exist, I want to add the count value. The resulting hash should be:

Result = { "abc" => { "def" => { "count" => 220 } } ,
            "sample" => { "samplecode" => { "count" => 3} } }
sawa
  • 165,429
  • 45
  • 277
  • 381
Imara
  • 11
  • 2
    What did you try? – Subash Apr 05 '18 at 07:27
  • 5
    “I want to convert...”—don’t hesitate to do what you want. – Aleksei Matiushkin Apr 05 '18 at 07:30
  • 3
    Welcome to Stack Overflow. Please take some time to read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and we will be able to help you. – McMutton Apr 05 '18 at 07:32
  • 2
    Post your code whatever you have tried. we are happy to assist you in resolving bugs/issues you encountered – user9405863 Apr 05 '18 at 07:33
  • 2
    This question's poster gets lambasted and downvoted, whilst another [poster](https://stackoverflow.com/q/49665891/5101493) (moments earlier) has a few answers to his seemingly ill-researched question without any criticism or any downvoting. – Sagar Pandya Apr 05 '18 at 07:44
  • Possible duplicate of [Convert hash keys to lowercase -- Ruby Beginner](https://stackoverflow.com/questions/7980206/convert-hash-keys-to-lowercase-ruby-beginner) – bork Apr 10 '18 at 19:31

1 Answers1

0

This would provide solution for your case: Next time it would be great if you could explain what method did you try and where did you face the issue in ..

result = {}
hash.each{|k,v|
  result[k.downcase] = result[k.downcase].present? ? result[k.downcase] : {}
  v.each{|key,values|
    result[k.downcase][key.downcase]= result[k.downcase][key.downcase].present? ? result[k.downcase][key.downcase] : {}
    values.each{|count, variable|
      result[k.downcase][key.downcase][count.downcase] = result[k.downcase][key.downcase][count.downcase].to_i + variable.to_i
    }
  }
}