Arrays 迭代散列/数组组合

Arrays 迭代散列/数组组合,arrays,ruby,hashmap,Arrays,Ruby,Hashmap,您需要通过哈希进行递归,这里有一个递归方法:- 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}"

您需要通过哈希进行递归,这里有一个递归方法:-

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"=>[{}]}]}]}}

我不知道您的问题是什么,但从代码中我猜您正在尝试遍历嵌套哈希和打印对,除非值是哈希或数组。您的代码不会将数组作为散列中的值来处理。以下是我的版本:

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

你的问题是什么?