如何将多维表从C函数返回到lua?

如何将多维表从C函数返回到lua?,c,arrays,lua,lua-table,C,Arrays,Lua,Lua Table,我需要像这样把一张桌子还给Lua: { [0] = { ["field1"] = "1", ["field2"] = "2" , ["field3"] = "3" }, [1] = { ["field1"] = "10" , ["field2"] = "20", ["field3"] = "30" } } 但是从C的角度来看,使用lua_*函数 另外,0和1只是一个示例,它可能包含更多类似的数组。 任何人都可以帮我吗?使用和的简单示例 我以为你在写某种包装。但还是差不多 /*

我需要像这样把一张桌子还给Lua:

{
    [0] = { ["field1"] = "1", ["field2"] = "2" , ["field3"] = "3" },
    [1] = { ["field1"] = "10" , ["field2"] = "20", ["field3"] = "30" }
}
但是从C的角度来看,使用lua_*函数 另外,0和1只是一个示例,它可能包含更多类似的数组。 任何人都可以帮我吗?

使用和的简单示例

我以为你在写某种包装。但还是差不多

/* Pushes multidimentional table on top of Lua VM stack. */
int
l_push_multidim_table(lua_State *L)
{
    /* Creates parent table of size 2 array elements: */
    lua_createtable(L, 2, 0);

    /* Puts key of the first child table on-top of Lua VM stack: */
    lua_pushnumber(L, 1);

    /*Creates first child table of size 3 non-array elements: */
    lua_createtable(L, 0, 3);

    /* Fills the first child table: */
    lua_pushnumber(L, 1);
    lua_setfield(L, -2, "field1");

    lua_pushnumber(L, 2);
    /* setfield() pops the value from Lua VM stack. */
    lua_setfield(L, -2, "field2");

    lua_pushnumber(L, 3);
    lua_setfield(L, -2, "field3");

    /* Remember, child table is on-top of the stack.
     * lua_settable() pops key, value pair from Lua VM stack. */
    lua_settable(L, -3);

    /* Pushes they key value for the second child table: */
    lua_pushnumber(L, 2);

    /*Creates second child table of size  3 non-array elements: */
    lua_createtable(L, 0, 3);

    /* Fills the second child table: */
    lua_pushnumber(L, 10);
    lua_setfield(L, -2, "field1");

    lua_pushnumber(L, 20);
    lua_setfield(L, -2, "field2");

    lua_pushnumber(L, 30);
    lua_setfield(L, -2, "field3");

    /* Remember, child table is still on-top of the stack.
     * lua_settable pops the key, value pair from Lua VM stack
     * And puts child table into the parent. */
    lua_settable(L, -3);

    /* Returns number of output tables:
     * (1 multidimentional)            */
    return 1;
}
注意:在Lua中,数组值通常从1开始。所以,我用这种方式整理了您的示例结构。总之,它应该运行良好。

使用和的简单示例

我以为你在写某种包装。但还是差不多

/* Pushes multidimentional table on top of Lua VM stack. */
int
l_push_multidim_table(lua_State *L)
{
    /* Creates parent table of size 2 array elements: */
    lua_createtable(L, 2, 0);

    /* Puts key of the first child table on-top of Lua VM stack: */
    lua_pushnumber(L, 1);

    /*Creates first child table of size 3 non-array elements: */
    lua_createtable(L, 0, 3);

    /* Fills the first child table: */
    lua_pushnumber(L, 1);
    lua_setfield(L, -2, "field1");

    lua_pushnumber(L, 2);
    /* setfield() pops the value from Lua VM stack. */
    lua_setfield(L, -2, "field2");

    lua_pushnumber(L, 3);
    lua_setfield(L, -2, "field3");

    /* Remember, child table is on-top of the stack.
     * lua_settable() pops key, value pair from Lua VM stack. */
    lua_settable(L, -3);

    /* Pushes they key value for the second child table: */
    lua_pushnumber(L, 2);

    /*Creates second child table of size  3 non-array elements: */
    lua_createtable(L, 0, 3);

    /* Fills the second child table: */
    lua_pushnumber(L, 10);
    lua_setfield(L, -2, "field1");

    lua_pushnumber(L, 20);
    lua_setfield(L, -2, "field2");

    lua_pushnumber(L, 30);
    lua_setfield(L, -2, "field3");

    /* Remember, child table is still on-top of the stack.
     * lua_settable pops the key, value pair from Lua VM stack
     * And puts child table into the parent. */
    lua_settable(L, -3);

    /* Returns number of output tables:
     * (1 multidimentional)            */
    return 1;
}
注意:在Lua中,数组值通常从1开始。所以,我用这种方式整理了您的示例结构。总之,它应该运行良好。

使用和的简单示例

我以为你在写某种包装。但还是差不多

/* Pushes multidimentional table on top of Lua VM stack. */
int
l_push_multidim_table(lua_State *L)
{
    /* Creates parent table of size 2 array elements: */
    lua_createtable(L, 2, 0);

    /* Puts key of the first child table on-top of Lua VM stack: */
    lua_pushnumber(L, 1);

    /*Creates first child table of size 3 non-array elements: */
    lua_createtable(L, 0, 3);

    /* Fills the first child table: */
    lua_pushnumber(L, 1);
    lua_setfield(L, -2, "field1");

    lua_pushnumber(L, 2);
    /* setfield() pops the value from Lua VM stack. */
    lua_setfield(L, -2, "field2");

    lua_pushnumber(L, 3);
    lua_setfield(L, -2, "field3");

    /* Remember, child table is on-top of the stack.
     * lua_settable() pops key, value pair from Lua VM stack. */
    lua_settable(L, -3);

    /* Pushes they key value for the second child table: */
    lua_pushnumber(L, 2);

    /*Creates second child table of size  3 non-array elements: */
    lua_createtable(L, 0, 3);

    /* Fills the second child table: */
    lua_pushnumber(L, 10);
    lua_setfield(L, -2, "field1");

    lua_pushnumber(L, 20);
    lua_setfield(L, -2, "field2");

    lua_pushnumber(L, 30);
    lua_setfield(L, -2, "field3");

    /* Remember, child table is still on-top of the stack.
     * lua_settable pops the key, value pair from Lua VM stack
     * And puts child table into the parent. */
    lua_settable(L, -3);

    /* Returns number of output tables:
     * (1 multidimentional)            */
    return 1;
}
注意:在Lua中,数组值通常从1开始。所以,我用这种方式整理了您的示例结构。总之,它应该运行良好。

使用和的简单示例

我以为你在写某种包装。但还是差不多

/* Pushes multidimentional table on top of Lua VM stack. */
int
l_push_multidim_table(lua_State *L)
{
    /* Creates parent table of size 2 array elements: */
    lua_createtable(L, 2, 0);

    /* Puts key of the first child table on-top of Lua VM stack: */
    lua_pushnumber(L, 1);

    /*Creates first child table of size 3 non-array elements: */
    lua_createtable(L, 0, 3);

    /* Fills the first child table: */
    lua_pushnumber(L, 1);
    lua_setfield(L, -2, "field1");

    lua_pushnumber(L, 2);
    /* setfield() pops the value from Lua VM stack. */
    lua_setfield(L, -2, "field2");

    lua_pushnumber(L, 3);
    lua_setfield(L, -2, "field3");

    /* Remember, child table is on-top of the stack.
     * lua_settable() pops key, value pair from Lua VM stack. */
    lua_settable(L, -3);

    /* Pushes they key value for the second child table: */
    lua_pushnumber(L, 2);

    /*Creates second child table of size  3 non-array elements: */
    lua_createtable(L, 0, 3);

    /* Fills the second child table: */
    lua_pushnumber(L, 10);
    lua_setfield(L, -2, "field1");

    lua_pushnumber(L, 20);
    lua_setfield(L, -2, "field2");

    lua_pushnumber(L, 30);
    lua_setfield(L, -2, "field3");

    /* Remember, child table is still on-top of the stack.
     * lua_settable pops the key, value pair from Lua VM stack
     * And puts child table into the parent. */
    lua_settable(L, -3);

    /* Returns number of output tables:
     * (1 multidimentional)            */
    return 1;
}

注意:在Lua中,数组值通常从1开始。所以,我用这种方式整理了您的示例结构。总而言之,它应该工作得很好。

为什么标记为
c++
?我标记为c,不是吗?为什么标记为
c++
?我标记为c,不是吗?为什么标记为
c++
?我标记为c,不是吗?为什么标记为
c++?我标记为c,不是吗?