2015-08-16 82 views
-5

你会希望通过哈希递归,这里是一个递归方法: -迭代散列/阵列组合

def ihash(h) 

    h.each_pair do |k,v| 

    if v.is_a?(Hash) 
     puts "key: #{k} recursing..." 
     ihash(v) 
    else 
     # MODIFY HERE! Look for what you want to find in the hash here 
     puts "key: #{k} value: #{v}" 
    end 
    end 
end 

然后你可以采取任何哈希,并传递它:

h = { 
"x" => "a", 
"y" => { 
    "y1" => { 
     "y2" => "final" 
    }, 
    "yy1" => "hello" 
}} 

ihash(h) 

但这下面哈希/阵列/散列组合不是与上面的代码工作

{"status"=>"REQUEST_SUCCEEDED", "responseTime"=>27, "message"=>[], "Results"=>{"series"=>[{"seriesID"=>"LAUCN040010000000005", "data"=>[{"year"=>"2015", "period"=>"M06", "periodName"=>"June", "value"=>"18444", "footnotes"=>[{"code"=>"P", "text"=>"Preliminary."}]}, {"year"=>"2015", "period"=>"M05", "periodName"=>"May", "value"=>"18443", "footnotes"=>[{}]}, {"year"=>"2015", "period"=>"M04", "periodName"=>"April", "value"=>"18012", "footnotes"=>[{}]}, {"year"=>"2015", "period"=>"M03", "periodName"=>"March", "value"=>"17701", "footnotes"=>[{}]}, {"year"=>"2015", "period"=>"M02", "periodName"=>"February", "value"=>"17549", "footnotes"=>[{}]}, {"year"=>"2015", "period"=>"M01", "periodName"=>"January", "value"=>"17632", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M12", "periodName"=>"December", "value"=>"17631", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M11", "periodName"=>"November", "value"=>"17668", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M10", "periodName"=>"October", "value"=>"17754", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M09", "periodName"=>"September", "value"=>"18095", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M08", "periodName"=>"August", "value"=>"18219", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M07", "periodName"=>"July", "value"=>"17860", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M06", "periodName"=>"June", "value"=>"18054", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M05", "periodName"=>"May", "value"=>"18011", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M04", "periodName"=>"April", "value"=>"17737", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M03", "periodName"=>"March", "value"=>"17436", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M02", "periodName"=>"February", "value"=>"17594", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M01", "periodName"=>"January", "value"=>"17562", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M12", "periodName"=>"December", "value"=>"17576", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M11", "periodName"=>"November", "value"=>"17251", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M10", "periodName"=>"October", "value"=>"17416", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M09", "periodName"=>"September", "value"=>"18010", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M08", "periodName"=>"August", "value"=>"18337", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M07", "periodName"=>"July", "value"=>"17840", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M06", "periodName"=>"June", "value"=>"18205", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M05", "periodName"=>"May", "value"=>"18307", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M04", "periodName"=>"April", "value"=>"17950", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M03", "periodName"=>"March", "value"=>"17736", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M02", "periodName"=>"February", "value"=>"17820", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M01", "periodName"=>"January", "value"=>"17956", "footnotes"=>[{}]}]}]}} 
+0

你的问题是什么? – sawa

回答

1

我不知道你的问题是什么,但从代码我会克因为你试图遍历嵌套散列和打印对,除非值是散列或数组。您的代码不会将数组作为散列中的值处理。这是我的版本:

def ihash(h) 
    if h.is_a?(Hash) 
    h.each_pair do |k, v| 
     puts "key: #{k} value: #{v}" unless [Hash, Array].include?(v.class) 
     puts "key: #{k} recursing..." 
     ihash(v) 
    end 
    elsif h.is_a?(Array) 
    h.each { |x| ihash(x) } 
    end 
end