【发布时间】:2011-09-02 12:29:56
【问题描述】:
我正在尝试遍历一个 lua 表,但我不断收到此错误:
invalid key to 'next'
我知道索引从 -8 开始,并且我知道那里有一个表,因为它获得了其中的第一个(也是唯一的)值。但是,即使我知道表中只有一个字符串,它也会尝试再次循环。
if (lua_istable(L, index))
{
lua_pushnil(L);
// This is needed for it to even get the first value
index--;
while (lua_next(L, index) != 0)
{
const char *item = luaL_checkstring(L, -1);
lua_pop(L, 1);
printf("%s\n", item);
}
}
else
{
luaL_typerror(L, index, "string table");
}
任何帮助将不胜感激。
当我使用正索引时,这很好用(只要我不从中删除 1)
编辑:我注意到,如果我不考虑 item 的值,我不会收到此错误。只有当我开始读取 item 的值时,我才会收到此错误。当我从表中得到值时,我调用另一个 Lua 函数,这会破坏 lua_next 吗?
【问题讨论】: