开发者

Resolv an error

a = [1,0,1]
b = [101101, 0101100, 1011001101, 11000111, 1010110]
i = 0
j = 0
c = []
x =开发者_开发技巧 0
d = []

while x < 5
  c.push b[i].to_s.split('')
  p c
  x = x +1
end

while i < 5
  e = c[i].length
  e = e + 1
  while j < e
    d[i][j] = a[i][j % 3] ^ b[i][j]
    puts d
    j = j + 1
  end
  i = i + 1
end


==> Resolv error line : d[i][j] = a[i][j % 3] ^ b[i][j]
====>  __.rb:18: undefined method `[]=' for nil:NilClass (NoMethodError)

I not see ..

Thanks


If you want to use d[i][j] you should create the 2 dimension:

...
d[i] = []
while j < e
  d[i][j] = a[i][j % 3] ^ b[i][j]
  puts d
  j = j + 1
end
...

but it's a strange code anyway. Why are you using a[i][j % 3] if a is a one-dimensional array?


Your variable d has only one dimension and in second while you're trying to assign two dimension value - d[i][j]. To resolve this issue put additional string to the while:

d[i] |= []
while j < e
    d[i][j] = a[i][j % 3] ^ b[i][j]
    puts d
    j = j + 1
  end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜