我制作了一个示例脚本.这是我正在运行的示例脚本:
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
Run Code Online (Sandbox Code Playgroud)
我得到以下输出和错误:
isotig00001_f1_3
in `[]': no implicit conversion of Symbol into Integer (TypeError)
Run Code Online (Sandbox Code Playgroud)
由于该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"}]}
Run Code Online (Sandbox Code Playgroud)
哈希是正确创建的.不过,我无法访问:cut_off和:d_value单独(可能的,因为他们是数字).我试过to_i,to_s方法等
saw*_*awa 15
neww不是哈希,而是数组[{:cut_off=>"11", :d_value=>"0.132"}].做
puts neww[0][:cut_off]
puts neww[0][:d_value]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23527 次 |
| 最近记录: |