我想通过一个Lua表进行迭代,但我不断收到此错误:迭代通过的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)这工作得很好
编辑:我注意到,我没有,如果得到这个错误我只留下物品的价值。只有当我开始阅读项目的价值时,我会得到这个错误。当我从表中获得价值时,我会调用另一个Lua函数,这是否会扰乱lua_next?
是,经过前后倾倒栈的内容,我注意到当我调用这个函数时还有剩下的东西。 – 2011-05-26 13:17:36