我正在使用此代码在嵌套哈希结构中填充值:
metrics_hash = Hash[ @metrics_array.map {|key| [key, nil] }]
demos_hash = Hash[@demos_types_array.map {|key| [key, metrics_hash] } ]
demos_hash.each do |demo , metric_hash |
metric_hash.each do |metric , value |
demo_index = @demos_types_array.index(demo)
metric_index = @metrics_array.index(metric)
offset = @demos_types_array.length()
value_index = metric_index * offset + demo_index
val = @demo_vals[value_index]
metric_hash[metric] = val
end
puts "#{demo.inspect} => #{metric_hash.inspect} "
end
row_hash = row_hash.merge!({"demos" => demos_hash } )
end
(为了使上面的代码更具可读性,我删除了一些打印语句。)
当我打印row_hash
时,其中的val
值demos_hash
是十进制数字,显示为"0"
:
demos"=>{"Ind.2+"=>{"AMA(000)"=>"0", "Shr% [Total TV Eng]"=>"0", "#Stations"=>"25"},
"A18+"=>{"AMA(000)"=>"0", "Shr% [Total TV Eng]"=>"0", "#Stations"=>"25"},
"A25-54"=> {"AMA(000)"=>"0", "Shr% [Total TV Eng]"=>"0", "#Stations"=>"25"},
"A18-49"=> {"AMA(000)"=>"0", "Shr% [Total TV Eng]"=>"0", "#Stations"=>"25"},
"A18-34"=> {"AMA(000)"=>"0", "Shr% [Total TV Eng]"=>"0", "#Stations"=>"25"},
"A55+"=> {"AMA(000)"=>"0", "Shr% [Total TV Eng]"=>"0", "#Stations"=>"25"},
"F25-54"=> {"AMA(000)"=>"0", "Shr% [Total TV Eng]"=>"0", "#Stations"=>"25"},
...........
"C2-11"=> {"AMA(000)"=>"0", "Shr% [Total TV Eng]"=>"0", "#Stations"=>"25"}}}
上面的大多数"0"
值都是以某种方式转换为的小数"0"
。请注意,车站的价值没有受到影响。上面的值是从这个数组中插入的:
demo_values = ["5.1", "5.1", "3.3", "3", "2.5", "1.9", "0", "0", "0", "3.3", "3",
"2.5", "2.5", "0", "0", "0.6", "0.6", "0.7", "0.6", "1.1", "0.7", "0", "0", "0", "1.4",
"1.2", "2.2", "0.9", "0", "0", "25", "25", "25", "25", "25", "25", "25", "25", "25",
"25", "25", "25", "25", "25", "25"]
注意这一行的输出:
puts "#{demo.inspect} => #{metric_hash.inspect} "
代码如下:
"A18-34" => {"AMA(000)"=>"2.5", "Shr% [Total TV Eng]"=>"1.1", "#Stations"=>"25"}
如您所见,数组中的大多数数字都是小数。
并且程序正在metrics_hash
正确填充。当我打印row_hash
包含demos_hash
上述内容的整个内容时,就会出现问题。可能会发生什么?
我检查了所有变量的类别并尝试输入数字但没有运气,我似乎无法取得进展。
有人可以帮助我了解如何进行吗?