我有一个嵌套哈希:
Hash = { "abc" => { "def" => { "count" => 120 } } ,
"ABC" => {"DEF" => { "COUNT" => 100 } },
"sample" => {"samplecode" => {"COUNT" => 3 } } }
我想将整个哈希转换为小写。如果存在任何重复项,我想添加计数值。生成的哈希值应为:
Result = { "abc" => { "def" => { "count" => 220 } } ,
"sample" => { "samplecode" => { "count" => 3} } }
答案 0 :(得分:0)
这将为您的案例提供解决方案: 下一次如果你能解释一下你尝试了什么方法以及你在哪里面对这个问题会很棒。
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
}
}
}