我在使用C迭代多维Lua表时遇到了问题。
让Lua表就是这样,即:
local MyLuaTable = {
{0x04, 0x0001, 0x0001, 0x84, 0x000000},
{0x04, 0x0001, 0x0001, 0x84, 0x000010}
}
我尝试扩展C示例代码:
/* table is in the stack at index 't' */
lua_pushnil(L); /* first key */
while (lua_next(L, t) != 0) {
/* uses 'key' (at index -2) and 'value' (at index -1) */
printf("%s - %s\n",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
第二个维度:
/* table is in the stack at index 't' */
lua_pushnil(L); /* first key */ /* Iterating the first dimension */
while (lua_next(L, t) != 0)
{
/* uses 'key' (at index -2) and 'value' (at index -1) */
printf("%s - %s\n",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
if(lua_istable(L, -1))
{
lua_pushnil(L); /* first key */ /* Iterating the second dimension */
while (lua_next(L, -2) != 0) /* table is in the stack at index -2 */
{
printf("%s - %s\n",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
}
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
输出结果为:
“数字 - 表格”(来自第一维)
“数字 - 数字”(来自第二维)
“数字 - 主题”(来自第二维)
之后我的代码崩溃了“while(lua_next(L,-2)!= 0)”
有人知道如何正确迭代二维Lua表吗?
答案 0 :(得分:3)
第二维的 while (lua_next(L, -2) != 0) /* table is in the stack at index -2 */
{
printf("%s - %s\n",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
}
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
在迭代之外!
lua_next()
你需要将它放在while循环中以使其工作以删除值,而你的while条件中的 while (lua_next(L, -2) != 0) /* table is in the stack at index -2 */
{
printf("%s - %s\n",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
隐式放入堆栈。
Calendar cal = Calendar.getInstance();
cal.setTime(jCalendar1.getDate());
int day = cal.get(Calendar.DAY_OF_MONTH);
JPanel jpanel = jCalendar1.getDayChooser().getDayPanel();
Component compo[] = jpanel.getComponents();
compo[day].setBackground(Color.red);
这样它应该按预期工作。