如何在C API中创建嵌套的Lua表

我想用 C API 创建一个如下的表格:

myTable = {
    [0] = { ["a"] = 4, ["b"] = 2 },
    [1] = { ["a"] = 13, ["b"] = 37 }
}

我的当前方法是在循环中创建内部表格:

lua_createtable(L, 0, 2);
int c = lua_gettop(L);
lua_pushstring(L, "a");
lua_pushnumber(L, 4);
lua_settable(L, c);
lua_pushstring(L, "b");
lua_pushnumber(L, 2);
lua_settable(L, c);

在此循环之前,我使用以下代码来创建包含 2 个数字插槽的外部表格:

lua_createtable(L, 2, 0);
int outertable = lua_gettop(L);

但是,我该如何将内部表格保存到外部表格中?

原文链接 https://stackoverflow.com/questions/1630169

点赞
stackoverflow用户126042
stackoverflow用户126042

下面是一个完整且简化的程序,演示了如何嵌套表格。基本上,您缺少的是 lua_setfield 函数。

#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

int main()
{
    int res;
    lua_State *L = lua_open();
    luaL_openlibs(L);

    lua_newtable(L); /* 底层表 */

    lua_newtable(L); /* 上层表 */

    lua_pushinteger(L, 4);
    lua_setfield(L, -2, "four"); /* T[four] = 4 */
    lua_setfield(L, -2, "T");  /* 将底层表的上层表字段命名为 T */
    lua_setglobal(L, "t"); /* 设置底层表为全局变量 t */

    res = luaL_dostring(L, "print(t.T.four == 4)");
    if(res)
    {
        printf("错误: %s\n", lua_tostring(L, -1));
    }

    return 0;
}

该程序将简单地打印“true”。

如果需要数字索引,则继续使用 lua_settable:

#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

int main()
{
    int res;
    lua_State *L = lua_open();
    luaL_openlibs(L);

    lua_newtable(L); /* 底层表 */

    lua_newtable(L); /* 上层表 */

    lua_pushinteger(L, 0);
    lua_pushinteger(L, 4);
    lua_settable(L, -3);  /* 将 4 存入上层表索引为 0 的位置;弹出 0 和 4 */
    lua_pushinteger(L, 0);
    lua_insert(L, -2); /* 将上层表(-2)与 0 交换位置 */
    lua_settable(L, -3); /* 底层表的索引为 0 的位置存放上层表 */
    lua_setglobal(L, "t"); /* 设置底层表为全局变量 t */

    res = luaL_dostring(L, "print(t[0][0] == 4)");
    if(res)
    {
        printf("错误: %s\n", lua_tostring(L, -1));
    }

    return 0;
}

与我所用的绝对索引 0 不同,您可能需要使用 lua_objlen 生成索引。

2009-10-27 11:39:18
stackoverflow用户107090
stackoverflow用户107090

对于你提供的简单代码,我的lua2c可以正常工作并生成以下代码。

/* 这个C代码是由下面的Lua代码使用lua2c生成的。

myTable = {
    [0] = { ["a"] = 4, ["b"] = 2 },
    [1] = { ["a"] = 13, ["b"] = 37 }
}
*/
static int MAIN(lua_State *L)
{
 lua_newtable(L);
 lua_pushnumber(L,0);
 lua_newtable(L);
 lua_pushliteral(L,"a");
 lua_pushnumber(L,4);
 lua_pushliteral(L,"b");
 lua_pushnumber(L,2);
 lua_settable(L,-5);
 lua_settable(L,-3);
 lua_pushnumber(L,1);
 lua_newtable(L);
 lua_pushliteral(L,"a");
 lua_pushnumber(L,13);
 lua_pushliteral(L,"b");
 lua_pushnumber(L,37);
 lua_settable(L,-5);
 lua_settable(L,-3);
 lua_settable(L,-5);
 lua_settable(L,-3);
 lua_setglobal(L,"myTable");
 return 0;
}
2009-10-28 00:10:55
stackoverflow用户2413135
stackoverflow用户2413135

这是我为解决类似问题而想出的一种通用方法,基于lhf的回答。这会创建一个Lua表单,格式如下:

{
    {"foo"},
    {"bar", "baz"}
}

其中包含任意长度的表格/子表格。

int list_of_lists_to_lua(lua_State* L, const std::vector<std::vector<std::string>>& convertme) {
    lua_newtable(L);
    int counter = 0;
    for (const std::vector<std::string>& list : convertme) {
        lua_pushnumber(L, ++counter);
        lua_newtable(L);
        int counter2 = 0;
        for (const std::string& item : list) {
            lua_pushnumber(L, ++counter2);
            lua_pushstring(L, item);
            lua_settable(L,-3);
        }
        lua_settable(L,-3);
    }
    return 1;
}
2015-02-24 14:03:06