2009-10-27 9 views
8

मैं सी एपीआई का उपयोग कर की तरहकैसे सी एपीआई

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

एक पाश में भीतरी टेबल बनाने के लिए है। इससे पहले, इस लूप, मैं

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

2 संख्यात्मक स्लॉट के लिए बाहरी तालिका बनाने के लिए उपयोग करता हूं।

लेकिन मैं आंतरिक तालिका को बाहरी तालिका में कैसे सहेज सकता हूं?

उत्तर

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 उपयोग करने के लिए सूचकांक उत्पन्न करने के लिए चाहते हो सकता है।

+0

lua_setfield के साथ संख्यात्मक सूचकांक कैसे बनाएं? – Etan

8

आपके द्वारा दिए गए कोड जैसे सरल कोड के लिए, मेरा 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; 
} 
2

यहां कुछ सामान्य है जो मैं एलएचएफ के उत्तर के आधार पर एक समान समस्या को हल करने के लिए आया हूं। यह फॉर्म

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

सी की तरह दिखता नहीं है न तो उचित रूप से संकलित करें। – Kamiccolo

+0

यह एरिस का उपयोग कर सी ++ है। – GunChleoc

संबंधित मुद्दे