2009-10-27 37 views
8

Tôi muốn tạo ra một bảng nhưLàm thế nào để tạo bảng Lua lồng nhau bằng cách sử dụng C API

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

sử dụng C API?

cách tiếp cận hiện tại của tôi là

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

để tạo ra các bảng bên trong một vòng lặp. Trước đây, vòng lặp này, tôi sử dụng

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

để tạo bảng ngoài cho 2 khe số.

Nhưng làm cách nào tôi có thể lưu các bảng bên trong vào bảng ngoài?

Trả lời

17

Đây là chương trình đầy đủ và tối thiểu thể hiện cách làm tổ. Về cơ bản những gì bạn đang thiếu là hàm 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; 
} 

Chương trình sẽ chỉ cần in true.

Nếu bạn cần chỉ số số, sau đó bạn tiếp tục sử dụng 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; 
} 

Thay vì sử dụng các chỉ số tuyệt đối từ 0 như tôi đã làm, bạn có thể muốn sử dụng lua_objlen để tạo ra các chỉ mục.

+0

cách tạo chỉ số bằng lua_setfield? – Etan

8

Đối với mã đơn giản như mã bạn đã cung cấp, lua2c hoạt động tốt và tạo mã bên dưới.

/* 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; 
} 
2

Đây là điều chung chung tôi đã giải quyết được vấn đề tương tự, dựa trên câu trả lời của lhf. Việc này sẽ tạo một bảng Lua cho biểu mẫu

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

Với độ dài bảng/bảng phụ tùy ý.

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

Không giống như C. Không biên dịch mà không bao gồm thích hợp. – Kamiccolo

+0

Đây là C++ sử dụng lệnh eris. – GunChleoc

Các vấn đề liên quan