2015-08-16 115 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