我有一个包含浮点值的数组(time
)。我想减去这个数组的元素并将结果存储在另一个数组中并进一步打印它。我尝试了以下方法:
time=Array.new
time=open("/home/sakshi/Desktop/test/PACKET-WISE_DETAILS/pkt_time.tsv", &:read)
time.each do |i|
puts time[i+1]-time[i]
end
但是我收到了这个错误:
'in `+': can't convert Fixnum into String (TypeError) ' error.
我还试图用while
循环重写上面的代码,但这也不起作用。
答案 0 :(得分:0)
这是因为你要向对象添加整数值,即id,试试这个
time = Array.new
time = open("/home/sakshi/Desktop/test/PACKET-WISE_DETAILS/pkt_time.tsv", &:read)
time.each_with_index do |i,index|
puts time[index+1]-time[index] if index < time.size #to avoid overflow when reaches the end
end