简体   繁体   English

将值推入哈希中的数组

[英]pushing a value into an array within a hash

Ive been working at this code for a radix sort for awhile now. 我已经在这段代码上进行基数排序了一段时间了。 Everything has seemed to be coming together fine until i try to push a value into an array within a hash. 直到我尝试将值推入哈希中的数组之前,一切似乎都很好。 Im getting an error message that the value is nil, however ive checked all the values in question just prior to trying to store them in the array and its still not allowing me to do so. 我收到一条错误消息,指出该值是nil,但是我还是在试图将所有值存储在数组中之前检查了所有有问题的值,但仍然不允许我这样做。 Any ideas? 有任何想法吗? buckets[sdigit].push(num) is the line that tells me one of my values is nil. buckets [sdigit] .push(num)是告诉我其中一个值为nil的行。

    arr = []
while arr.size < 100
  arr.push(rand(1000))
end
for outer_index in arr
  puts "for outer index(#{outer_index} in arr"
  buckets = Hash.new()
  puts "buckets = Hash.new()"
  puts "for j in 0..9"
  for j in 0..9
    buckets[j.to_s] = Array.new()
    #buckets[j.to_s] = [j]
    puts "buckets[j.to_s(#{buckets[j.to_s]})"
  end

for inner_index in arr
  puts "for inner_index(#{inner_index}) in arr"
  num = inner_index
  puts "num(#{num}) = inner_index(#{inner_index})"

  sdigit = num.to_s[-1]
  puts "sdigit(#{sdigit}) = num.to_s[-1](#{num.to_s[-1]})"
  digit = sdigit.to_i
  puts "digit(#{digit}) = sdigit.to_i(#{sdigit.to_i})"
  puts "buckets[digit] = #{buckets[sdigit]}"
  puts "#{buckets["1"]}"
  puts "o#{num}"
  puts buckets
 buckets[sdigit].push(num)
  puts "buckets[digit].push(num)(#{buckets[digit].push(num)})"

end
  arr = buckets.values.flatten

end `

buckets[sdigit].push(num) is the line that tells me one of my values is nil. buckets [sdigit] .push(num)是告诉我其中一个值为nil的行。

If you look at the error message : 如果您查看错误消息:

top.rb:30:in `block (2 levels) in <main>': undefined method `push' for nil:NilClass (NoMethodError)

and your code : 和您的代码:

在此处输入图片说明

you see that line 30 is the puts , not buckets[sdigit].push(num) . 您会看到第30行是puts ,而不是buckets[sdigit].push(num)

Cause : there is a discrepancy between the values displayed by the puts and those used by the expression : [digit] instead of [sdigit] , and that's the trace which causes the error. 原因: puts所显示的值与表达式所使用的值之间存在差异: [digit]而不是[sdigit] ,这就是导致错误的踪迹。

Personally I write the puts traces before the statement to trace, because it shows the values that will be used by the expression before the statement which could cause the error is executed. 我个人将puts跟踪写在要跟踪的语句之前,因为它显示了语句之前该表达式将使用的值,这可能会导致错误执行。 It usually helps ... except when the trace itself is in error. 它通常有帮助……除非跟踪本身有错误。

I have slightly rearranged your code : 我稍微重新排列了您的代码:

arr = []

100.times { arr << rand(1000) }
puts arr.join(', ')

arr.each do | outer_index |
  puts "===== for outer_index=#{outer_index} in arr"
  buckets = Hash.new()
  puts "buckets = Hash.new()"

  puts "for j in 0..9"
  (0..9).each do | j |
    buckets[j.to_s] = Array.new()
    #buckets[j.to_s] = [j]
    puts "buckets[#{j.to_s}]=#{buckets[j.to_s]}"
  end

  arr.each do | inner_index |
    puts "----- for inner_index=#{inner_index} in arr"
    num = inner_index
    puts "num(#{num}) = inner_index(#{inner_index})"

    sdigit = num.to_s[-1]
    puts "sdigit(#{sdigit}) = num.to_s[-1](#{num.to_s[-1]})"
    digit = sdigit.to_i
    puts "digit(#{digit}) = sdigit.to_i(#{sdigit.to_i})"
    puts "buckets[digit] = #{buckets[sdigit]}"
    puts "#{buckets["1"]}"
    puts "o#{num}"
    puts buckets

    puts "buckets[sdigit].push(num)=buckets[#{sdigit}].push(#{num})"
    buckets[sdigit].push(num)
#    puts "buckets[digit].push(num)(#{buckets[digit].push(num)})"
  end

  arr = buckets.values.flatten
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM