Ruby中的嵌套数组结构

时间:2013-07-26 12:23:50

标签: ruby arrays hash

Restaurant = {:name=>"McDonalds",
 :location=>"NYC",
 :chefs=>
  [{:name=>"Sunny", :food=>"fries"},
   {:name=>"Brooklyn", :food=>"burgers"},
   {:name=>"Mac", :food=>"burgers"}],
 :waiters=>
  [{:name=>"Jess", :role=>"senior manager"},
   {:name=>"Sam", :role=>"manager"},
   {:name=>"Jack", :role=>"server"},
   {:name=>"Mary", :role=>"server"}]}

如何打印出上面数组中的所有值?

3 个答案:

答案 0 :(得分:1)

Restaurant = {:name=>"McDonalds",
 :location=>"NYC",
 :chefs=>
  [{:name=>"Sunny", :food=>"fries"},
   {:name=>"Brooklyn", :food=>"burgers"},
   {:name=>"Mac", :food=>"burgers"}],
 :waiters=>
  [{:name=>"Jess", :role=>"senior manager"},
   {:name=>"Sam", :role=>"manager"},
   {:name=>"Jack", :role=>"server"},
   {:name=>"Mary", :role=>"server"}]}

keys = Restaurant.map{|k,v| k if v.is_a? Array}.compact

keys.each{|i| Restaurant[i].each{|h| p h.keys,h.values}}

<强>输出:

[:name, :food]
["Sunny", "fries"]
[:name, :food]
["Brooklyn", "burgers"]
[:name, :food]
["Mac", "burgers"]
[:name, :role]
["Jess", "senior manager"]
[:name, :role]
["Sam", "manager"]
[:name, :role]
["Jack", "server"]
[:name, :role]
["Mary", "server"]

keys.each{|i| Restaurant[i].each{|h| p h.to_a.flatten}}

<强>输出:

[:name, "Sunny", :food, "fries"]
[:name, "Brooklyn", :food, "burgers"]
[:name, "Mac", :food, "burgers"]
[:name, "Jess", :role, "senior manager"]
[:name, "Sam", :role, "manager"]
[:name, "Jack", :role, "server"]
[:name, "Mary", :role, "server"]

keys.each{|i| Restaurant[i].each{|h| p h.to_a}}

<强>输出

[[:name, "Sunny"], [:food, "fries"]]
[[:name, "Brooklyn"], [:food, "burgers"]]
[[:name, "Mac"], [:food, "burgers"]]
[[:name, "Jess"], [:role, "senior manager"]]
[[:name, "Sam"], [:role, "manager"]]
[[:name, "Jack"], [:role, "server"]]
[[:name, "Mary"], [:role, "server"]]

<强>更新

要求'pp'

hsh = {:name=>"McDonalds",
 :location=>"NYC",
 :chefs=>
  [{:name=>"Sunny", :food=>"fries"},
   {:name=>"Brooklyn", :food=>"burgers"},
   {:name=>"Mac", :food=>"burgers"}],
 :waiters=>
  [{:name=>"Jess", :role=>"senior manager"},
   {:name=>"Sam", :role=>"manager"},
   {:name=>"Jack", :role=>"server"},
   {:name=>"Mary", :role=>"server"}]}

pp hsh.flat_map{|k,v| v.is_a?(Array) ? (v.flat_map(&:values)) : v }

<强>输出

["McDonalds",
 "NYC",
 "Sunny",
 "fries",
 "Brooklyn",
 "burgers",
 "Mac",
 "burgers",
 "Jess",
 "senior manager",
 "Sam",
 "manager",
 "Jack",
 "server",
 "Mary",
 "server"]

puts hsh.flat_map{|k,v| v.is_a?(Array) ? (v.flat_map(&:values)) : v }.join(",")

# => McDonalds,NYC,Sunny,fries,Brooklyn,burgers,Mac,burgers,Jess,senior manager,Sam,manager,Jack,server,Mary,server

答案 1 :(得分:0)

puts(
  Restaurant
  .values.flatten.flat_map{|v| v.kind_of?(Hash) ? v.values : v}
  .join(", ")
)

# => McDonalds, NYC, Sunny, fries, Brooklyn, burgers, Mac, burgers, Jess, senior manager, Sam, manager, Jack, server, Mary, server

答案 2 :(得分:0)

这是有点格式化输出的示例。正如其他人所问,关于你想要什么样的输出有一个很大的问题。

Restaurant = {:name=>"McDonalds",
 :location=>"NYC",
 :chefs=>
  [{:name=>"Sunny", :food=>"fries"},
   {:name=>"Brooklyn", :food=>"burgers"},
   {:name=>"Mac", :food=>"burgers"}],
 :waiters=>
  [{:name=>"Jess", :role=>"senior manager"},
   {:name=>"Sam", :role=>"manager"},
   {:name=>"Jack", :role=>"server"},
   {:name=>"Mary", :role=>"server"}]}

def print_restaurant( res, level = 0, indent = 3 )
  res.each do |rk, rv|
    if rv.is_a? Array then
      puts "\n" + (" " * indent * level) + "#{rk.to_s}:"
      rv.each { |cv| print_restaurant(cv, level + 1) }
    else
      puts (" " * indent * level) + "#{rk.to_s}: #{rv}"
    end
  end
end

print_restaurant( Restaurant )

name: McDonalds
location: NYC

chefs:
   name: Sunny
   food: fries
   name: Brooklyn
   food: burgers
   name: Mac
   food: burgers

waiters:
   name: Jess
   role: senior manager
   name: Sam
   role: manager
   name: Jack
   role: server
   name: Mary
   role: server