invalid key to 'next'
stack traceback:
[C]: in function 'next'
Main:45: in function 'collisions'
Main:36: in function 'draw'
Wondering what can cause this. I am doing something somewhat iffy:
A double loop for _,a in iPairs(table) do for _,b in ipairs(table) do and possibly setting an element in table to nil as the loop runs. I was hoping that if this happened, the loop next would just provide a nil in a or b, whichever runs across the nil.
I’ve also seen the same message when NOT doing the remove in the loop, so pretty sure the nil-setting isn’t doing it.
Reusing the same index will cause that error. For the inner loop, try a double underscore. _ has no special meaning, it’s just a variable like any other.
Actually, I take it back. Having done some experiments, it seems it is ok (tho maybe not advisable?) to mess around with the index variable in the loop, and nesting loops with the same index variable seems to work OK too (tho I would advise against it). I think @RonJeffries we might have to ask whether you could show us a minimal working example (or should that be minimal non-working example)?
In your post at the top, you write ipairs, but then further down, you give a key-value dictionary {a=1, b=2, c=3} . Did you mean pairs (without the i)?
@RonJeffries Are you running something like this. I’m deleting the “b” table entry when a=2 and b=2 in the loops. I’m also using pairs instead of ipairs. I’m not getting any errors if I keep running this.
function setup()
tab={a=1,b=2,c=3}
print("pass 1")
for _,a in pairs(tab) do
for _,b in pairs(tab) do
print(a,b)
if a==2 and b==2 then
tab["b"]=nil
end
end
end
print("pass 2")
for _,a in pairs(tab) do
for _,b in pairs(tab) do
print(a,b)
end
end
end
When the bug is erratic, you need to isolate the code that is causing it, and, if other people are helping, provide a sample that produces the bug (maybe erratically, but it’s in there somewhere).
@Ignatz i cannot cause the bug anything like reliably. i’ve seen it maybe three times on a good day. thus i inquire whether anyone happens to know what goes on in the C next well enough to say “that happens when …”.
failing that, as obviously i have, i’ll just do things some other way. the crash will go away and we won’t find the problem in Codea until next time …