lua-table

Passing table with fields as an argument to Lua function from C++?

梦想与她 提交于 2019-12-23 01:53:08
问题 I would like to know how to form a Lua table with fields and values so I can pass it as an argument to a Lua function from C++. I know how to form a table using indices but I don't know how to from a table made of fields and values. For example, I want to send this table to a Lua function as an argument from C++. t = {xpos = 50, ypos = 80, message = 'hello'} The below code is the closest I could get, but it's just indexed table with no field name. lua_getglobal(L, "myLuaFunc"); if (lua_type(L

Get Lua table size in C

徘徊边缘 提交于 2019-12-22 05:22:01
问题 How can I get a size of a Lua table in C? static int lstage_build_polling_table (lua_State * L) { lua_settop(L, 1); luaL_checktype(L, 1, LUA_TTABLE); lua_objlen(L,1); int len = lua_tointeger(L,1); printf("%d\n",len); ... } My Lua Code: local stages = {} stages[1] = stage1 stages[2] = stage2 stages[3] = stage3 lstage.buildpollingtable(stages) It´s printing 0 always. What am I doing wrong? 回答1: lua_objlen returns the length of the object, it doesn't push anything on the stack. Even if it did

Get Lua table size in C

泪湿孤枕 提交于 2019-12-22 05:21:55
问题 How can I get a size of a Lua table in C? static int lstage_build_polling_table (lua_State * L) { lua_settop(L, 1); luaL_checktype(L, 1, LUA_TTABLE); lua_objlen(L,1); int len = lua_tointeger(L,1); printf("%d\n",len); ... } My Lua Code: local stages = {} stages[1] = stage1 stages[2] = stage2 stages[3] = stage3 lstage.buildpollingtable(stages) It´s printing 0 always. What am I doing wrong? 回答1: lua_objlen returns the length of the object, it doesn't push anything on the stack. Even if it did

Print table in Lua

本秂侑毒 提交于 2019-12-21 21:31:13
问题 I have a script in Lua and I need to print the variable res, but I do not know how to do this print. I get the result of the function in another function, where I wanted to do print function parseCSVLine(line) local res = {} local pos = 1 local sep = ',' while true do local c = string.sub(line,pos,pos) if (c == "") then break end if (c == '"') then -- quoted value (ignore separator within) local txt = "" repeat local startp,endp = string.find(line,'^%b""',pos) -- Digitos txt = txt..string.sub

What's the difference between table.insert(t, i) and t[#t+1] = i?

为君一笑 提交于 2019-12-20 11:07:09
问题 In Lua, there seem to be two ways of appending an element to an array: table.insert(t, i) and t[#t+1] = i Which should I use, and why? 回答1: Which to use is a matter of preference and circumstance: as the # length operator was introduced in version 5.1, t[#t+1] = i will not work in Lua 5.0, whereas table.insert has been present since 5.0 and will work in both. On the other hand, t[#t+1] = i uses exclusively language-level operators, wheras table.insert involves a function (which has a slight

how to delete all elements in a Lua table?

China☆狼群 提交于 2019-12-20 10:24:28
问题 How do I delete all elements inside a Lua table? I don't want to do: t = {} table.insert(t, 1) t = {} -- this assigns a new pointer to t I want to retain the same pointer to t, but delete all elements within t . I tried: t = {} table.insert(t, 1) for i,v in ipairs(t) do table.remove(t, i) end Is the above valid? Or is something else needed? 回答1: for k in pairs (t) do t [k] = nil end Will also work - you may have difficulty with ipairs if the table isn't used as an array throughout. 回答2: Table

Lua script to return efficient dictionary from Redis HGETALL call

断了今生、忘了曾经 提交于 2019-12-19 08:43:06
问题 I need to use Redis HMGET from a Lua script and extract specific values in following code. But redis.call('HMGET', table_key, hkey1, hkey2, ...) return a flat array of {hkey1, val1, hkey2, val2, ...} To extract values by key I wrote: local function flat_map_get(flat_map, hash_key) local i = 1 while flat_map[i] do if flat_map[i] == hash_key then return flat_map[i+1] end i = i+2 end end Of course, as usage grow, multiple calls to this function presented major performance drop. What is an

How to work with tables passed as an argument to a lua C function?

不羁岁月 提交于 2019-12-19 03:42:22
问题 I'm going to implement a function with C language and which will be called by Lua script. This function should receive a lua table as the argument, so I should read the fields in the table.I try to do like below, but my function is crashing when I run it. Can anyone help my find the problem? /* function findImage(options) imagePath = options.imagePath fuzzy = options.fuzzy ignoreColor = options.ignoreColor; end Call Example: findImage {imagePath="/var/image.png", fuzzy=0.5, ignoreColor

How do I create a Lua Table in C++, and pass it to a Lua function?

核能气质少年 提交于 2019-12-17 22:53:39
问题 In C++, I have a map<string, string> , containing an unknown number of entries. How can I pass this to a Lua function, so that the Lua function can use the data as a table? 回答1: if you want a real lua table: lua_newtable(L); int top = lua_gettop(L); for (std::map::iterator it = mymap.begin(); it != mymap.end(); ++it) { const char* key = it->first.c_str(); const char* value = it->second.c_str(); lua_pushlstring(L, key, it->first.size()); lua_pushlstring(L, value, it->second.size()); lua

One loop for iterating through multiple Lua tables

泪湿孤枕 提交于 2019-12-14 04:19:26
问题 Is it possible to iterate through multiple Lua tables with the same loop? For looping through indexed tables I can do something like this: local t1 = {"a", "b", "c"} local t2 = {"d", "e", "f"} local num = #t1+#t2 for i=1, num, do local j local val if i <= #t1 then j = i val = t1[j] else j = i-#t1 val = t2[j] end -- Do stuff end but how about key-value tables? E.g. something like this: local t1 = {a="a", b="b", c="c"} local t2 = {d="d", e="e", f="f"} for key, val in pairs(t1) or pairs(t2) do