Ruby遍历哈希哈希

时间:2018-06-29 10:26:07

标签: ruby

我有以下数组,正在努力格式化以满足我的需求。

consolidated = [
  {:name=>"Bob",   :details=>{"work"=>"Carpenter", "age"=>"26", "Experience"=>"6"} },
  {:name=>"Colin", :details=>{"work"=>"painting",  "age"=>"20", "Experience"=>"4"} }
]

我正在尝试将其格式化为以下格式:

Bob    work         Carpenter
       age          26
       Experience   6

Colin  work         painting
       age          20
       Experience   4

我尝试了以下操作:

require 'csv'
CSV.open("output.csv", "wb") do |csv|
  csv << ["name", "nature", "details"]
  consolidated.each do |val|
    csv << [val[:name], val[:details]]
  end
end
  #=> [{:name=>"Bob",   :details=>{"work"=>"Carpenter", "age"=>"26", "Experience"=>"6"}},
  #    {:name=>"Colin", :details=>{"work"=>"painting", "age"=>"20", "Experience"=>"4"}}]

但会显示以下内容

name      nature                                                  details

Bob      "work"=>"Carpenter", "age"=>"26", "Experience"=>"6"

Colin    "work"=>"painting", "age"=>"20", "Experience"=>"4"

我不确定如何从第1个循环中迭代散列的散列,只得到所需的格式。

谢谢。

3 个答案:

答案 0 :(得分:4)

以下是一些入门指南:

require 'csv'
data = [
  {:name => "Bob", :details=>{"work"=>"Carpenter", "age"=>"26", "Experience"=>"6"}},
  {:name => "Colin", :details=>{"work"=>"painting", "age"=>"20", "Experience"=>"4"}}
]

str = CSV.generate do |csv|
  data.each do |datum|
    datum[:details].each do |detail_key, detail_value|
      csv << [datum[:name], detail_key, detail_value]
    end
  end
end

puts str
# >> Bob,work,Carpenter
# >> Bob,age,26
# >> Bob,Experience,6
# >> Colin,work,painting
# >> Colin,age,20
# >> Colin,Experience,4

只需迭代所有细节,并为那里的每个键值对发出一个新行,并添加一个人的名字。

这将使您几乎所需。在各行和个人姓名之间仅缺少空白行在每行中重复。找出如何添加这些改进是您的家庭作业。

答案 1 :(得分:1)

我不知道CSV的生成(因此,假设它按您编写的方式工作),您可以通过以下方式迭代对象:

consolidated = [{:name => "Bob", :details=>{"work"=>"Carpenter", "age"=>"26", "Experience"=>"6"}}, {:name => "Colin", :details=> {"work"=>"painting", "age"=>"20", "Experience"=>"4"}}]


CSV.open("output.csv", "wb") do |csv|
  csv << ["name", "nature", "details"]

  consolidated.each do |val|
    details = val[:details]
    nature_1 = details.keys.first
    detail_1 = details.delete(nature_1)
    csv << [val[:name], nature_1, detail_1]

    details.each do |k, v|
      csv << [nil, k, v]
    end
  end
end

注意:这将破坏您的原始数据数组consolidated。因此,如果要保留它,请先dup。或修改逻辑以不从val[:details]中删除第一个键值。

答案 2 :(得分:0)

您需要通过each_pair迭代器来迭代嵌入的哈希。 像这样:

data = {:name => "Bob", :details=>{"work"=>"Carpenter", "age"=>"26", "Experience"=>"6"}}

CSV.open("output.csv", "wb") do |csv|

  csv << ["name", "nature", "details"]

  data.each do |val|

    csv << [ val[:name], val[:details]['work'] ]

    data[:details].each_pair do |key, value]

      # here we have to drop the first pair because i've used it earlier 
      next if key == 'work'

      csv << [ "", key, value ]

    end

  end

end