我正在为 Ruby 中的数据结构而苦苦挣扎。
我有:
answers = [
{"val"=>["6"], "comment"=>["super"], "qid"=>["2"]},
{"val"=>["3"], "comment"=>[""], "qid"=>["1"]},
{"val"=>["7"], "comment"=>[""], "qid"=>["4"]},
{"val"=>["5", "6"], "comment"=>["supera", "hmm"], "qid"=>["1", "2"]},
{"val"=>["5", "9"], "comment"=>["super", "asdf"], "qid"=>["1", "5"]}
]
我需要以下 qid 数组,它们应该是唯一的,在所有哈希中:
["2","1","4","5"] # note, value 2 exists two times and value 1, 3 times
相应的值应汇总并除以计数:
["12","13","7","9"] will be: ["6","4.3","7","9"] # 12/2 and 13/3
还应总结评论:
[["super","hmm"],["","supera","super"],[""],["asdf"]]
我想知道把它放在一个哈希中是否很酷?
到目前为止,我有:
a = Hash.new(0)
answers.each.map { |r| r }.each do |variable|
variable["qid"].each_with_index do |var, index|
#a[var] << { :count => a[var][:count] += 1 }
#a[var]["val"] += variable["val"][index]
#a[var]["comment"] = a[var]["comment"].to_s + "," + variable["comment"][index].to_s
end
end
我正在尝试为Highcharts Demo - Basic bar生成数据。宝石是LazyHighCharts
有任何想法吗?建议?
编辑:
也许我必须再次解释一下结构:有问题 id(qid),每个问题都有一个值和一个注释,我试图计算“val”哈希的平均值