Posted 27 February 2012 - 03:29 AM
According to the Lua manual, under documentation for the next() function:
This statement does not restrict the modification, including deletion, of existing elements to only those that are not the current element. I have verified on my native install of Lua that deleting the current element works fine:
If I run the same code in ComputerCraft Lua, it fails:
This thus appears to violate the specification, and in a rather annoying way since this would make it much easier to delete elements from a table that match a particular predicate.
The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields.
This statement does not restrict the modification, including deletion, of existing elements to only those that are not the current element. I have verified on my native install of Lua that deleting the current element works fine:
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> t = {2,4,6}
> for k,v in pairs(t) do print(k, ",", v) if v == 4 then t[k]=nil end end
1 , 2
2 , 4
3 , 6
> for k,v in pairs(t) do print(k, ",", v) if v == 4 then t[k]=nil end end
1 , 2
3 , 6
>
If I run the same code in ComputerCraft Lua, it fails:
Interactive Lua prompt.
Call exit() to exit.
lua> t = {2,4,6}
lua> for k,v in pairs(t) do print(k, ",", v) if v == 4 then t[k]=nil end end
1,2
2,4
lua:1: invalid key to 'next'
This thus appears to violate the specification, and in a rather annoying way since this would make it much easier to delete elements from a table that match a particular predicate.