문제

나는 같은 테이블을 만들고 싶다

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 숫자 슬롯의 외부 테이블을 만들려면.

그러나 내부 테이블을 외부 테이블에 어떻게 저장할 수 있습니까?

도움이 되었습니까?

해결책

다음은 테이블 중첩 방법을 보여주는 완전하고 최소한의 프로그램입니다. 기본적으로 당신이 놓친 것은입니다 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 인덱스를 생성합니다.

다른 팁

당신이 준 것과 같은 간단한 코드를 위해 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;
}

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top