lua-table

ServiceStack Redis, how to return Lua table as List

妖精的绣舞 提交于 2019-12-09 12:33:53
问题 I'm using the Redis client from ServiceStack. I have a Lua script that fills up a Lua table with results from several Redis calls. I want to return this table in some way. My thought was to use the method ExecLuaShaAsList from the client lib and in the lua script just do a "return myTable". It doesnt work, I always get an empty list back. How to return the lua table to the redis client? This the C# script I use with the Redis client: using (var redisClient = GetPooledRedisClient()) { var sha1

Pushing a Lua table

柔情痞子 提交于 2019-12-09 11:47:48
问题 I have created a Lua table in C , but I'm not sure how to push that table onto the top of a stack so I can pass it to a Lua function. Does anyone know how to do this? This is my current code: lua_createtable(state, libraries.size(), 0); int table_index = lua_gettop(state); for (int i = 0; i < libraries.size(); i++) { lua_pushstring(state, libraries[i].c_str()); lua_rawseti(state, table_index, i + 1); } lua_settable(state, -3); [ Push other things ] [ Call function ] 回答1: Here's a quick helper

Redis - Lua tables as return values - why is this not working

匆匆过客 提交于 2019-12-09 10:15:24
问题 When I run this code through redis EVAL it return no results. Any idea why this is not working? redis-cli EVAL "$(cat bug.lua)" 0 bug.lua local retv = {} retv["test"] = 1000 return retv If I initialize the table that value alone gets printed. $ cat bug.lua --!/usr/bin/env lua local retv = {"This", "is", "a", "bug" } retv["test"] = 1000 return retv $ redis-cli EVAL "$(cat bug.lua)" 2 a b 1) "This" 2) "is" 3) "a" 4) "bug" 回答1: If you refer to the Redis EVAL documentation you can see what are

sorting a table in descending order in Lua

落花浮王杯 提交于 2019-12-08 23:26:32
I can not get it work: tbl = { [1] = { ['etc2'] = 14477 }, [2] = { ['etc1'] = 1337 }, [3] = { ['etc3'] = 1336 }, [4] = { ['etc4'] = 1335 } } for i = 1, #tbl do table.sort(tbl, function(a, b) return a[i] > b[i] end) print(tbl[i] .. '==' .. #tbl) end Getting this error: attempt to compare two nil values This is a follow-on to table value sorting in lua How about this? tbl = { { 'etc3', 1336 }, { 'etc2', 14477 }, { 'etc4', 1335 }, { 'etc1', 1337 }, } table.sort(tbl, function(a, b) return a[2] > b[2] end) for k,v in ipairs(tbl) do print(v[1], ' == ', v[2]) end Organizing the data that way made it

sorting a table in descending order in Lua

99封情书 提交于 2019-12-08 04:39:01
问题 I can not get it work: tbl = { [1] = { ['etc2'] = 14477 }, [2] = { ['etc1'] = 1337 }, [3] = { ['etc3'] = 1336 }, [4] = { ['etc4'] = 1335 } } for i = 1, #tbl do table.sort(tbl, function(a, b) return a[i] > b[i] end) print(tbl[i] .. '==' .. #tbl) end Getting this error: attempt to compare two nil values This is a follow-on to table value sorting in lua 回答1: How about this? tbl = { { 'etc3', 1336 }, { 'etc2', 14477 }, { 'etc4', 1335 }, { 'etc1', 1337 }, } table.sort(tbl, function(a, b) return a

How to read Lua table return value from C++

有些话、适合烂在心里 提交于 2019-12-07 10:34:58
问题 I have a Lua function that returns table (contains set of strings) the function run fine using this code: lua_pushstring (lua, "funcname"); lua_gettable (lua, LUA_GLOBALSINDEX); lua_pushstring(lua, "someparam"); lua_pcall (lua, 1, 1, 0); the function returns a table. How do I read it's contents from my C++ code? 回答1: If you are asking how to traverse the resulting table, you need lua_next (the link also contains an example). As egarcia said, if lua_pcall returns 0, the table the function

Lua - Execute a Function Stored in a Table

和自甴很熟 提交于 2019-12-07 03:20:52
问题 I was able to store functions into a table. But now I have no idea of how to invoke them. The final table will have about 100 calls, so if possible, I'd like to invoke them as if in a foreach loop. Thanks! Here is how the table was defined: game_level_hints = game_level_hints or {} game_level_hints.levels = {} game_level_hints.levels["level0"] = function() return { [on_scene("scene0")] = { talk("hint0"), talk("hint1"), talk("hint2") }, [on_scene("scene1")] = { talk("hint0"), talk("hint1"),

Popping the first element off an array

谁说我不能喝 提交于 2019-12-06 23:45:26
问题 I have an array x in Lua. I would like to set head = x[1] and rest = the rest of the array, so that rest[1] = x[2] , rest[2] = x[3] , etc. How can I do this? (note: I don't care if the original array gets mutated. In Javascript I would do head = x.shift() and x would contain the remaining elements.) 回答1: head = table.remove(x, 1) "Pop" is a bit of a misnomer, as it implies a cheap operation, and removing the first element of an table requires relocating the rest of the contents--hence the

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

跟風遠走 提交于 2019-12-06 15:36:55
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, -1) == LUA_TFUNCTION) { lua_newtable(L); lua_pushinteger(L, 1); lua_pushnumber(L, 50); lua

How to print Lua table using the redefined print function?

安稳与你 提交于 2019-12-06 07:37:41
I learned how to redefine the Lua's print() in C++ from this post. ( https://stackoverflow.com/a/4514193/5224286 ) Here's the redefined print function that prints variables to my host program's console. (through the functions named post.. ) int l_my_print(lua_State *L) { int nargs = lua_gettop(L); for (int i = 1; i <= nargs; ++i) { if (lua_isnil(L, i)) poststring("nil"); else if (lua_isboolean(L, i)) lua_toboolean(L, i) ? poststring("true") : poststring("false"); else if (lua_isnumber(L, i)) postfloat(static_cast<t_float>(lua_tonumber(L, i))); else if (lua_isstring(L, i)) poststring(lua