lua-table

'Magic Square' algorithm

折月煮酒 提交于 2019-12-04 05:28:59
问题 As an experiment, I'm trying to create a magic square program that checks every possible square with nine numbers. For those who do not know, a magic square is a 3x3 grid of numbers 1-9, where each row, column, and diagonal add up to 15. For example: How would I go about checking each square using a table with Lua? I'm starting with the following table: local sq = { 1, 1, 1, 1, 1, 1 1, 1, 1 } How would I go about checking each table in the correct order? I'm able to draw out my thinking on

Dynamically assigned table variables?

时光怂恿深爱的人放手 提交于 2019-12-04 04:35:45
问题 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

Lua in pairs with same order as it's written

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 03:35:33
问题 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

ServiceStack Redis, how to return Lua table as List

佐手、 提交于 2019-12-03 15:30:26
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 = redisClient.LoadLuaScript(luaBody); List<string> theList = redisClient.ExecLuaShaAsList(sha1); int

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

走远了吗. 提交于 2019-12-03 14:08:08
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" If you refer to the Redis EVAL documentation you can see what are the rules Redis uses to convert a Lua table into a Redis reply: Lua table (array) -> Redis multi bulk reply (

Pushing a Lua table

筅森魡賤 提交于 2019-12-03 12:57:36
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 ] Here's a quick helper function to push strings to the table void l_pushtablestring(lua_State* L , char* key , char* value) { lua

How can I check if a lua table contains only sequential numeric indices?

筅森魡賤 提交于 2019-12-03 07:23:16
How can I write a function that determines whether it's table argument is a true array? isArray({1, 2, 4, 8, 16}) -> true isArray({1, "two", 3, 4, 5}) -> true isArray({1, [3]="two", [2]=3, 4, 5}) -> true isArray({1, dictionaryKey = "not an array", 3, 4, 5}) -> false I can't see any way of finding out if the numeric keys are the only keys. ipairs iterates over indices 1..n, where n+1 is the first integer index with a nil value pairs iterates over all keys. if there are more keys than there are sequential indices, then it cannot be an array. So all you have to do is see if the number of elements

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

久未见 提交于 2019-12-03 01:11:54
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? 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 amount of overhead to look up and call and depends on the table module in the environment). In the second

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

我的梦境 提交于 2019-12-02 14:06:56
问题 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

use “#” operator to calculate the length of a table in Lua? [duplicate]

北战南征 提交于 2019-12-02 04:23:30
问题 This question already has answers here : How to get number of entries in a Lua table? (7 answers) Closed 4 years ago . I have a question when I use # to calculte the length of a table. For Example: local t = {"a", "b"} local t1 = {"a", nil} print(#t) -- 2 print(#t1) -- 1 local t = {"a", "b"} local t1 = {nil, "a"} print(#t) -- 2 print(#t1) -- 2 can someone tell me why it is? 回答1: Unless __len metamethod is defined, # operator can only be operated on a table that is a sequence. A sequence is, a