Error in (C) Next:

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.

So … thoughts?

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.

interesting idea, @yojimbo2000, why would that be? any idea? anyway worth doing just for drill …

Makes a hash of the variable lookup table would be my guess.

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)?

What would stop it from falling out of both looks after a single pass through the inner loop?

try it. just loop over {a=1, b=2, c=3} twice. loops 9x. but in MY loop, once in a long long while, it gets the next error shown.

@RonJeffries - I would try running the code without deleting any entries, and see if that makes a difference

Can you show a small example so we can try it.

yes. I’m sure I can record the loop so it won’t explode, but i’d really like to understand what is happening.

@dave1707 i haven’t been able to make it happen reliably.

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()
    print("pass 1")
    for _,a in pairs(tab) do
        for _,b in pairs(tab) do
            if a==2 and b==2 then
    print("pass 2")
    for _,a in pairs(tab) do
        for _,b in pairs(tab) do

@RonJeffries Its hard to try and figure out a problem if we have to guess at what the code looks like.

@dave1707 it is. but the crash does not occur every time, or even often. my question really was what could cause this C-level error to happen at all.

@yojimbo2000 yes i meant pairs, sorry.

Ron, you know the drill.

There is no single source of C bugs.

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 …