Lua find a key from a value
If you find yourself needing to get the key from the value of a table, consider inverting the table as in function table_invert(t) local s={} for k,v in pairs(t) do s[v]=k end return s end
If you find yourself needing to get the key from the value of a table, consider inverting the table as in function table_invert(t) local s={} for k,v in pairs(t) do s[v]=k end return s end
There are generally two ways to obfuscate Lua source code: Obfuscate the code directly, mostly by renaming variables, introducing istraction and restructuring code to be harder to follow Encode the source code and embed it as a string in a Lua file that only decodes, loads and runs the encoded real program. In reality, a … Read more
If the requirement is “quick and dirty” I’ve found this one useful. Because if the recursion it can print nested tables too. It doesn’t give the prettiest formatting in the output but for such a simple function it’s hard to beat for debugging. function dump(o) if type(o) == ‘table’ then local s=”{ ” for k,v … Read more
You can use Lua’s native ‘execute’ command. Example: os.execute(“c:\\temp\\program.exe”) Sources: Lua Guide / os.execute
There was a problem in the NodeMCU docs with the socket:send example you seem to have based your implementation on. We discussed it and I fixed it. An improved version of your code is this: gpio.mode(3, gpio.OUTPUT) srv = net.createServer(net.TCP, 28800) print(“Server created… \n”) local pinState = 0 srv:listen(80, function(conn) conn:on(“receive”, function(sck, request) local _, … Read more
I hate having to install libraries (especially those that want me to use installer packages to install them). If you’re looking for a clean solution for a directory listing on an absolute path in Lua, look no further. Building on the answer that sylvanaar provided, I created a function that returns an array of all … Read more
Here is an implementation of a locals() function. It will return a table of locals from the calling scope: function locals() local variables = {} local idx = 1 while true do local ln, lv = debug.getlocal(2, idx) if ln ~= nil then variables[ln] = lv else break end idx = 1 + idx end … Read more
the general case of iterating over an array and removing random items from the middle while continuing to iterate If you’re iterating front-to-back, when you remove element N, the next element in your iteration (N+1) gets shifted down into that position. If you increment your iteration variable (as ipairs does), you’ll skip that element. There … Read more
You already have the solution in the question — the only way is to iterate the whole table with pairs(..). function tablelength(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end Also, notice that the “#” operator’s definition is a bit more complicated than that. Let … Read more
About tables in general (oh, can’t you just give me an array) In Lua, a table is the single general-purpose data structure. Table keys can be of any type, like number, string, boolean. Only nil keys aren’t allowed. Whether tables can or can’t contain nil values is a surprisingly difficult question which I tried to … Read more