lua-table

Proper way to free a pointer array in SWIG input typemap?

偶尔善良 提交于 2019-12-02 03:19:37
Hi I'm trying to wrap the following function using SWIG. static void readTable(int argc, t_atom *argv) { //accepts table in Lua e.g. readTable({"ab",3}); for (int i=0; i<argc; ++i) { if (argv[i].a_type == A_FLOAT) printf("FLOAT : %g\n", argv[i].a_w.w_float); else if (argv[i].a_type == A_SYMBOL) printf("SYMBOL : %s\n", argv[i].a_w.w_symbol->s_name); } } Here's the typemap I created. %include "exception.i" %typemap(in) (int argc, t_atom *argv) { if (!lua_istable(L, 1)) { SWIG_exception(SWIG_RuntimeError, "argument mismatch: table expected"); } lua_len(L, 1); $1 = lua_tointeger(L, -1); $2 = (t

Why length is different in Lua

為{幸葍}努か 提交于 2019-12-01 21:51:31
I'm learning Lua for corona sdk and I have these local type1 = {nil, "(", nil, "x" ,nil , ")" ; n=6} local type2 = {"(",nil, "x",nil, ")",nil ; n=6} print(#type1) --prints 6 print(#type2) --prints 3 why the second one is not 6 too?? The # operator doesn't work on every table, it works only on a sequence, that is, the set of its positive numeric keys is equal to {1..n} for some integer n . In that case, n is its length. For instance, local t = {"hello", 42, true} is a sequence. But both your tables are not sequence because they have "holes" of nil . See Lua 5.2 Reference Manual: The length

Lua in pairs with same order as it's written

这一生的挚爱 提交于 2019-12-01 21:05:12
Is there any way to loop trough a table like the one below in the same order as it's written? local tbl = { ["hello"] = 1, [2] = 2, [50] = 3, ["bye"] = 4, [200] = 5 } What I mean is that when I use "in pairs" I'll get a different order everytime I execute my code ... I'm searching for something like this: function get_keys(tbl) local rtable = {} for k,v in pairs(tbl) do table.insert(rtable, k) end return rtable end local keys_of_tbl = get_keys(tbl) for i = 1, table.getn(keys_of_tbl) do --Do something with: tbl[keys_of_tbl[i]] end But because the function "get_keys" is based on "in pairs" again

Dynamically assigned table variables?

和自甴很熟 提交于 2019-12-01 20:31:43
Writing a function in Lua, which creates two tables. I want the tables to be assigned to the value name with an x added, and one with a y added. For example if name was line, it would create two tables linex and liney, but I can't figure out how to do it. The following obviously doesn't work (and is just for display purposes) but how would I go about doing this? function makelinep(x,y,minrand,maxrand,name,length) name..x = {} name..y = {} Later I hope to access "linex" and "liney" after values have been written. Joel If you want these in the global name space you would use _G[name..'x']={} _G

Lua : remove duplicate elements

一曲冷凌霜 提交于 2019-12-01 16:40:13
i am having a table in lua test = {1,2,4,2,3,4,2,3,4,"A", "B", "A"} I want to remove all duplicate elements in table. Output should be test = {1,2,4,3,"A","B"} EDIT: My try : > items = {1,2,4,2,3,4,2,3,4,"A", "B", "A"} > flags = {} > for i=1,table.getn(items) do if not flags[items[i]] then io.write(' ' .. items[i]) flags[items[i]] = true end >> end 1 2 4 3 A B> It is working fine now. Thanks for answers and comments. Similar to example given by @Dimitry but only one loop local test = {1,2,4,2,3,4,2,3,4,"A", "B", "A"} local hash = {} local res = {} for _,v in ipairs(test) do if (not hash[v])

method for serializing lua tables

走远了吗. 提交于 2019-12-01 15:59:18
问题 I may have missed this, but is there a built-in method for serializing/deserializing lua tables to text files and vice versa? I had a pair of methods in place to do this on a lua table with fixed format (e.g. 3 columns of data with 5 rows). Is there a way to do this on lua tables with any arbitrary format? For an example, given this lua table: local scenes={ {name="scnSplash", obj={ { name="bg", type="background", path="scnSplash_bg.png", }, { name="bird", type="image", path="scnSplash_bird

table.unpack() only returns the first element [duplicate]

[亡魂溺海] 提交于 2019-12-01 15:58:57
This question already has an answer here: Lua unpack() messing arguments 1 answer Could somebody explain to me why table.unpack() returns the first table element only when it is used in a function call with additional parameters after table.unpack() ? Here is some demo code: local a = {1,2,3,4,5} print("Test", table.unpack(a)) -- prints "Test 1 2 3 4 5" print(table.unpack(a), "Test") -- prints "1 Test" I don't understand why the second line just prints 1 Test . I'd expect it to print 1 2 3 4 5 Test . Can somebody explain this behaviour? I'd also be interested in how I can make the second call

table.unpack() only returns the first element [duplicate]

穿精又带淫゛_ 提交于 2019-12-01 14:59:15
问题 This question already has an answer here : Lua unpack() messing arguments (1 answer) Closed 4 years ago . Could somebody explain to me why table.unpack() returns the first table element only when it is used in a function call with additional parameters after table.unpack() ? Here is some demo code: local a = {1,2,3,4,5} print("Test", table.unpack(a)) -- prints "Test 1 2 3 4 5" print(table.unpack(a), "Test") -- prints "1 Test" I don't understand why the second line just prints 1 Test . I'd

How can I safely iterate a lua table while keys are being removed

▼魔方 西西 提交于 2019-12-01 03:39:00
In my main coroutine, I am removing or adding entries from a table depending on user operations. In the background, I'd like to iterate over the entries in the table. I don't mind particularly if I miss an insertion on one iteration, providing I can catch it before the next. Is it safe to iterate over it with pairs ? Or should I use next instead? You can safely remove entries while traversing a table but you cannot create new entries, that is, new keys. You can modify the values of existing entries, though. (Removing an entry being a special case of that rule.) JUST MY correct OPINION You can

How can I safely iterate a lua table while keys are being removed

扶醉桌前 提交于 2019-12-01 00:25:50
问题 In my main coroutine, I am removing or adding entries from a table depending on user operations. In the background, I'd like to iterate over the entries in the table. I don't mind particularly if I miss an insertion on one iteration, providing I can catch it before the next. Is it safe to iterate over it with pairs ? Or should I use next instead? 回答1: You can safely remove entries while traversing a table but you cannot create new entries, that is, new keys. You can modify the values of