如何从哈希中提取数组值?

时间:2013-05-14 21:58:35

标签: ruby-on-rails ruby data-structures highcharts

我正在努力使用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”哈希的平均值

3 个答案:

答案 0 :(得分:0)

好的,我想我明白你要拍的是什么......

# lets create a hash to store the data
# such that my_data[qid][0] => value sum
#           my_data[qid][1] => number of times qid appeared
#           my_data[qid][2] => comments array

my_data = {}

# go through answers and fill my_data out

answers.each do |h|
    for i in 0...h["qid"].length
        qid = h["qid"][i]

        # awesome ruby syntax that will set my_data[qid] 
        # only if it hasn't already been set using "or-equals" operator
        my_data[qid] ||= [0, 0, []] 

        my_data[qid][0] += h["val"][i].to_i # add value
        my_data[qid][1] += 1                # increment count
        my_data[qid][2] << h["comment"][i]  # add comment
    end
 end

# how to get the data out for qid of "2"

my_data["2"][0].to_f / my_data["2"][1] # => 6
my_data["2"][2]                        # => ["super", "hmm"]

# and we could do this process for any qid, or all qids by iterating over
# my_data. we could even build the arrays in your OP

qids = []
average_values = []
comments = []

my_data.each do |k, v|
    qids << k
    average_values << v[0].to_f / v[1]
    comments << v[2]
end

# qids           => ["2", "1", "4", "5"]
# average_values => [6, 4.3, 7, 9]
# comments       => [["super", hmm"] ["", "supera", "super"], [""], ["asdf"]]

答案 1 :(得分:0)

qid

result_qid = answers.map{|i| i['qid']}.flatten.uniq!
#["2", "1", "4", "5"]

result_qid = answers.map{|i| i['qid']}.flatten.group_by{|i| i}.map{|i,j| [i,j.length]}
#[["2", 2], ["1", 3], ["4", 1], ["5", 1]]

我无法弄清楚相应值的逻辑!

答案 2 :(得分:0)

如果在Hash中存储数据受到如此限制,您可以定义一个新类并管理该类中的数据处理。我会有这样的事情:

class AnswerParser
  def intialize(answers)
    #your implementation here
  end

  def qids
    #your implementation here
  end

  def comments
    #your implementation here
  end
  ...

end

#usage
parser = AnswerParser.new(anwsers)
qids = parser.qids
...

通过这种方式,您可以将代码分开,以便于测试和使用。

希望这有帮助。

相关问题