如何使用C API创建嵌套的Lua表格

8
我想创建一个类似的表格:
myTable = {
    [0] = { ["a"] = 4, ["b"] = 2 },
    [1] = { ["a"] = 13, ["b"] = 37 }
}

使用C API吗?

我的当前方法是

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);

在循环中创建内部表格。在此循环之前,我使用

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

创建一个包含2个数字插槽的外部表格。

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

3个回答

17
这是一个完整和最简程序,演示如何嵌套表格。基本上你缺少的是 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); /* bottom table */

    lua_newtable(L); /* upper table */

    lua_pushinteger(L, 4);
    lua_setfield(L, -2, "four"); /* T[four] = 4 */
    lua_setfield(L, -2, "T");  /* name upper table field T of bottom table */
    lua_setglobal(L, "t"); /* set bottom table as global variable t */

    res = luaL_dostring(L, "print(t.T.four == 4)");
    if(res)
    {
        printf("Error: %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); /* bottom table */

    lua_newtable(L); /* upper table */

    lua_pushinteger(L, 0);
    lua_pushinteger(L, 4);
    lua_settable(L, -3);  /* uppertable[0] = 4; pops 0 and 4 */
    lua_pushinteger(L, 0);
    lua_insert(L, -2); /* swap uppertable and 0 */
    lua_settable(L, -3); /* bottomtable[0] = uppertable */
    lua_setglobal(L, "t"); /* set bottom table as global variable t */

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

    return 0;
}

与我之前使用的绝对索引0不同,您可能希望使用lua_objlen生成索引。


如何使用lua_setfield创建数字索引? - Etan

9

对于像你提供的简单代码,我的lua2c很好用,并生成下面的代码。

/* This C code was generated by lua2c from the Lua code below.

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;
}

3

下面是我为解决类似问题而想出的一种通用方法,基于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;
}

看起来不像是C语言。如果没有正确的包含,也无法编译。 - Kamiccolo
这是使用 Eris 的 C++。 - GunChleoc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接