从 Lua 到 C 访问嵌套表以获取值

嗨,我正在尝试从 Lua 到 C 访问一个嵌套表。 这个表格是:

arg =
{
  MagicNumber =  {MagicNumber, 0},
  ProdNum   =  {ProdNum,    1},
  LetterR   =  {LetterR,      0xc},
  Revision  =  {Revision,    0xd},
  Space1    =  {Space1,     0xe},
  MnfctrCode  =  {MnfctrCode,  0xf},
  Hyphen1   =  {Hyphen1,      0x12},
  ZeroCode  =  {ZeroCode, 0x13},
  Hyphen2   =  {Hyphen2,      0x15},
  MnfctrMnth  =  {MnfctrMnth,  0x16},
  MnfctrYear  =  {MnfctrYear,  0x18},
  SerialNum =  {SerialNum,  0x1a},
  Space2    =  {Space2,     0x1e},
  ChkSum    =  {ChkSum,      0x1f},
}

里面的表格都是整数值,并且表格中的键是一个字符串。我的代码片段如下:

lua_pushnil(L);

while(lua_next(L, -2) != 0)
{
    field = lua_tostring(L, -2);
    printf("\n %d field = %s", i, field);
    wrData[i-1] = lua_tonumber(L,-1);
    printf("\n data = 0x%x", wrData[i-1]);
    lua_pop(L, -1);
    i++;
}

我错过了什么吗?因为我得到的值是 0x0

点赞
用户7066323
用户7066323

将下面翻译成中文并且保留原本的 markdown 格式:

   void luaAccess(lua_State * L)// call this function with the table on the top of the stack
    {
        lua_pushnil(L);
        while(lua_next(L, -2))
        {
            switch(lua_type(L, -2))
            {
                case LUA_TSTRING:
                    //deal with the outer table index here
                break;
                //...
                // deal with aditional index types here
            }
            switch(lua_type(L, -1))
            {
                case LUA_TTABLE:
                    lua_pushnil(L);
                    while (lua_next)
                    {
                        switch(lua_type(L, -2))
                        {
                            case LUA_TNUMBER:
                                // deal with the inner table index here
                            break;
                            //...
                            // deal with aditional index types here
                        }
                        switch(lua_type(L, -1))
                        {
                            case LUA_TSTRING:
                                //deal with strings
                            break;
                            case LUA_TNUMBER
                                //deal with numbers
                            break;
                        }
                        lua_pop(L, 1);
                    }
                //...
                // deal with aditional index types here
                break
            }
        }
        lua_pop(L, 1);
    }

将该函数更改以适应您的需求,并使用栈顶的表调用它。

void luaAccess(lua_State * L) // 使用栈顶的表调用此函数
{
    lua_pushnil(L); // 将一个 nil 值压入栈顶

    while(lua_next(L, -2)) // 遍历栈顶表的键值对
    {
        switch(lua_type(L, -2)) // 判断键类型
        {
            case LUA_TSTRING:
                // 在此处理外部表索引
                break;
            //...
            // 还可以在这里处理其他类型的键
        }

        switch(lua_type(L, -1)) // 判断值类型
        {
            case LUA_TTABLE:
                lua_pushnil(L); // 将一个 nil 值压入栈顶
                while (lua_next) // 遍历内部表的键值对
                {
                    switch(lua_type(L, -2)) // 判断键类型
                    {
                        case LUA_TNUMBER:
                            // 在此处理内部表索引
                            break;
                        //...
                        // 还可以在这里处理其他类型的键
                    }
                    switch(lua_type(L, -1)) // 判断值类型
                    {
                        case LUA_TSTRING:
                            // 在此处理字符串
                            break;
                        case LUA_TNUMBER:
                            // 在此处理数字
                            break;
                    }
                    lua_pop(L, 1); // 弹出栈顶元素
                }
            //...
            // 还可以在这里处理其他类型的值
            break;
        }
    }
    lua_pop(L, 1); // 弹出栈顶元素
}
2017-01-07 23:00:35