在while循环中从数组和变量创建哈希数组

时间:2015-06-07 19:33:48

标签: arrays ruby hash while-loop

来自这样的数组:

@upc = ['123ab', '456cd']

和一组在while循环中确定的变量,在第一次迭代期间,它们将如下所示:

@vendor = "Nike"
@type = "Running"
@color = "Blue"

在第二次迭代期间,可能如下所示:

@vendor = "Converse"
@type = "Hi-Top"
@color = "Red"

我想得到这个:

[
  {:upc=>"123ab", :vendor=>"Nike", :type=>"Running", :color=>"Blue"},
  {:upc=>"456cd", :vendor=>"Converse", :type=>"Hi-Top", :color=>"Red"}
]

我正在使用此代码:

final_hash =
@upc.map{|upc| {:upc=> upc, :vendor => @vendor, :type => @type, :color => @color}}

但我最终将变量放入一次,然后像这样复制:

[
  {:upc=>"123ab", :vendor=>"Nike", :type=>"Running", :color=>"Blue"},
  {:upc=>"456cd", :vendor=>"Nike", :type=>"Running", :color=>"Blue"}
]

如何将数组和变量放入哈希数组?

3 个答案:

答案 0 :(得分:0)

final_hash = @upc.map{|upc| {:upc=> upc, :vendor => @vendor, :type => @type, :color => @color}}

这不会起作用,因为它只会使用@vendor@type@color的最后一个值。

如果您有以下代码:

@vendor = "Nike"
@type = "Running"
@color = "Blue"

@vendor = "Converse"

@vendor的第一个值被最后一个值覆盖。

您可能希望将数据存储在数组中:

@shoe_1 = { vendor: "Nike", type: "Running", color: "Blue" }
@shoe_2 = ...
@shoes = [@shoe_1, @shoe_2]

现在您可以合并数组:

@shoes.map.with_index{| shoe, i | shoe.merge upc:@upc[ i ]}
=> [{
    :vendor => "Nike",
      :type => "Running",
     :color => "Blue",
       :upc => "123ab"
   },...

答案 1 :(得分:0)

正如@BSeven建议的那样,您可能应该使用不同的数据结构,一种适合您希望使用该信息的方式。例如,一种可能性可能是:

h = { "Nike"     => { "Running" => { "Blue"=>"123ab", "Black"=>"123ac" },
                      "Walking" => { "Pink"=>"124ab", "Mauve"=>"124af" } },
      "Converse" => { "Hi-Top"  => { "Red" =>"456cd", "Green"=>"457cg"  },
                      "Running" => { "Red" =>"457cd", "Black"=>"457ch" } } }

这样您就可以轻松回答以下类型的问题:

Nike跑鞋的颜色是什么颜色,每种颜色的upc代码是什么?

h["Nike"]["Running"] 
  #=> {"Blue"=>"123ab", "Black"=>"123ac"} 

哪些制造商制作黑色跑步节目?

h.select { |_, types|
  types.key?("Running") && types["Running"].key?("Black") }.keys
  #=> ["Nike", "Converse"]

所有黑鞋的制造商,型号和upc代码是什么?

h.each_with_object([]) do |(mfg, types), ans|
  types.each do |type, colors|
    ans << { mfg: mfg, type: type, upc: colors["Black"] } if colors.key?("Black")
  end
end
  #=> [{:mfg=>"Nike",     :type=>"Running", :upc=>"123ac"},
  #    {:mfg=>"Converse", :type=>"Running", :upc=>"457ch"}] 

哪只鞋有upc&#34; 456cd&#34;?

upc = h.each_with_object({}) do |(mfg, types), g|
  types.each do |type, colors|
     colors.each { |color, upc| g[upc] = [mfg: mfg, type: type, color: color] }
  end
end
  #=> {"123ab"=>[{:mfg=>"Nike",     :type=>"Running", :color=>"Blue" }],
  #    "123ac"=>[{:mfg=>"Nike",     :type=>"Running", :color=>"Black"}],
  #    "124ab"=>[{:mfg=>"Nike",     :type=>"Walking", :color=>"Pink" }],
  #    "124af"=>[{:mfg=>"Nike",     :type=>"Walking", :color=>"Mauve"}],
  #    "456cd"=>[{:mfg=>"Converse", :type=>"Hi-Top",  :color=>"Red"  }],
  #    "457cg"=>[{:mfg=>"Converse", :type=>"Hi-Top",  :color=>"Green"}],
  #    "457cd"=>[{:mfg=>"Converse", :type=>"Running", :color=>"Red " }],
  #    "457ch"=>[{:mfg=>"Converse", :type=>"Running", :color=>"Black"}]} 
upc["456cd"]
  #=> [{:mfg=>"Converse", :type=>"Hi-Top", :color=>"Red"}] 

这与您的问题只是切线相关,但我认为您可能会发现它很有用。

答案 2 :(得分:0)

你的while - 循环必须控制upc元素的迭代。

final_hash = []
upc = @upc.dup
while whatover_condition_you_have
  final_hash.push({upc: upc.shift, vendor: @vendor, type: @type, color: @color})
end