lua:lua
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
lua:lua [2008/09/01 15:42] – deva | lua:lua [2012/04/20 13:53] (current) – deva | ||
---|---|---|---|
Line 1: | Line 1: | ||
=====LUA===== | =====LUA===== | ||
- | ====c/c++==== | + | ====C/C++==== |
+ | ===Arrays=== | ||
<code lua> | <code lua> | ||
myarray = {1,2,3}; | myarray = {1,2,3}; | ||
Line 20: | Line 21: | ||
// and then some code to bind it to " | // and then some code to bind it to " | ||
+ | </ | ||
+ | |||
+ | ===Array of arrays=== | ||
+ | An array with 10 entries each containing 2 entries: | ||
+ | < | ||
+ | int num = 10; | ||
+ | int sz = 2; | ||
+ | | ||
+ | lua_createtable(L, | ||
+ | int toptop = lua_gettop(L); | ||
+ | |||
+ | for(int i = 1; i <= num; i++) { | ||
+ | lua_createtable(L, | ||
+ | int top = lua_gettop(L); | ||
+ | |||
+ | for(int j = 1; j <= sz; j++) { | ||
+ | char buf[10]; | ||
+ | sprintf(buf, | ||
+ | lua_pushstring(L, | ||
+ | lua_rawseti(L, | ||
+ | } | ||
+ | |||
+ | lua_rawseti(L, | ||
+ | } | ||
+ | |||
+ | return 1; | ||
+ | </ | ||
+ | |||
+ | ===Fields (maps)=== | ||
+ | <code lua> | ||
+ | foo = {} | ||
+ | foo.hello = ' | ||
+ | foo.bar = {} | ||
+ | foo.bar.ping = ' | ||
+ | foo.bar.bas = {} | ||
+ | foo.bar.bas.ding = ' | ||
+ | </ | ||
+ | |||
+ | Corresponds to: | ||
+ | <code c> | ||
+ | int foo, bar, bas; | ||
+ | |||
+ | // Create global ' | ||
+ | lua_newtable(L); | ||
+ | lua_setglobal(L, | ||
+ | |||
+ | // Set foo.hello = ' | ||
+ | lua_getglobal(L, | ||
+ | foo = lua_gettop(L); | ||
+ | lua_pushstring(L, | ||
+ | lua_setfield(L, | ||
+ | |||
+ | // Create ' | ||
+ | lua_getglobal(L, | ||
+ | foo = lua_gettop(L); | ||
+ | lua_newtable(L); | ||
+ | lua_setfield(L, | ||
+ | |||
+ | // Set foo.bar.ping = ' | ||
+ | lua_getglobal(L, | ||
+ | foo = lua_gettop(L); | ||
+ | lua_getfield(L, | ||
+ | bar = lua_gettop(L); | ||
+ | lua_pushstring(L, | ||
+ | lua_setfield(L, | ||
+ | |||
+ | // Create ' | ||
+ | lua_getglobal(L, | ||
+ | foo = lua_gettop(L); | ||
+ | lua_getfield(L, | ||
+ | bar = lua_gettop(L); | ||
+ | lua_newtable(L); | ||
+ | lua_setfield(L, | ||
+ | |||
+ | // Set foo.bar.bas.ding = ' | ||
+ | lua_getglobal(L, | ||
+ | foo = lua_gettop(L); | ||
+ | lua_getfield(L, | ||
+ | bar = lua_gettop(L); | ||
+ | lua_getfield(L, | ||
+ | bas = lua_gettop(L); | ||
+ | lua_pushstring(L, | ||
+ | lua_setfield(L, | ||
+ | |||
+ | lua_pop(L, 1); // pop ' | ||
</ | </ |
lua/lua.1220276575.txt.gz · Last modified: 2008/09/01 15:42 by deva