ruby 使用散列获取类型错误(没有将符号隐式转换为整数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19786942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:17:19  来源:igfitidea点击:

Geting a Typeerror with hashes (No implicit conversion of symbol into integer)

rubyhash

提问by Ismail Moghul

I made a sample script. Here is the sample script I am running:

我做了一个示例脚本。这是我正在运行的示例脚本:

def array_generator
  signalp_array = Array.new(11){ Array.new(11,0) }
  signalp = Hash.new
  file = File.readlines("./sample.txt")
  file.each_with_index do |line, idx|
    row = line.gsub(/\s+/m, ' ').chomp.split(" ") # split the line into a array based on white space.
    signalp_array[idx][0..row.length - 1] = row # Merge into existing array
  end
  signalp_array.each do |g|
    seq_id = g[0] 
    cut_off = g[4]
    d_value = g[8]
    signalp[seq_id] = [:cut_off => cut_off, :d_value => d_value] 
  end
  return signalp
 end

signalp = array_generator

puts signalp 
signalp.each do |id, neww|
  puts id
  puts neww[ :cut_off]
  puts neww[ :d_value]
end

with which I am getting the following output and error:

我得到以下输出和错误:

isotig00001_f1_3
in `[]': no implicit conversion of Symbol into Integer (TypeError)

Since the puts signalpline gives me the following:

由于该puts signalp行给了我以下内容:

{"isotig00001_f1_3"=>[{:cut_off=>"11", :d_value=>"0.132"}], "isotig00001_f1_5"=>[{:cut_off=>"11", :d_value=>"0.162"}], "isotig00001_f1_7"=>[{:cut_off=>"11", :d_value=>"0.397"}], "isotig00001_f1_8"=>[{:cut_off=>"11", :d_value=>"0.259"}], "isotig00001_f1_9"=>[{:cut_off=>"11", :d_value=>"0.110"}], "isotig00001_f1_10"=>[{:cut_off=>"11", :d_value=>"0.135"}], "isotig00001_f1_11"=>[{:cut_off=>"1", :d_value=>"0.000"}], "isotig00001_f1_12"=>[{:cut_off=>"12", :d_value=>"0.117"}], "isotig00001_f2_0"=>[{:cut_off=>"11", :d_value=>"0.108"}], "isotig00001_f2_1"=>[{:cut_off=>"28", :d_value=>"0.122"}], "isotig00001_f2_3"=>[{:cut_off=>"19", :d_value=>"0.097"}]}

the hash is created properly. However I cannot access the :cut_offand :d_valueindividually (probably, because they are digits). I tried to_i, to_smethods etc.

哈希已正确创建。但是我无法单独访问:cut_off:d_value(可能,因为它们是数字)。我试过to_ito_s方法等。

  1. Could someone let me know what I am doing wrong?
  2. Any ideas on what to search for or where to learn more on the topic?
  1. 有人可以让我知道我做错了什么吗?
  2. 关于要搜索什么或在哪里可以了解有关该主题的更多信息的任何想法?

回答by sawa

newwis not a hash, it is an array [{:cut_off=>"11", :d_value=>"0.132"}]. Do

neww不是散列,它是一个数组[{:cut_off=>"11", :d_value=>"0.132"}]。做

puts neww[0][:cut_off]
puts neww[0][:d_value]

回答by Chowlett

The values in your newwhash are arraysof hashes, not just bare hashes. You need to index into the array before keying into the hash. That is:

neww散列中的值是散列数组,而不仅仅是裸散列。在键入散列之前,您需要对数组进行索引。那是:

puts neww[0][:cut_off]