Load Lua-files by relative path

There is a way of deducing the “local path” of a file (more concretely, the string that was used to load the file). If you are requiring a file inside lib.foo.bar, you might be doing something like this: require ‘lib.foo.bar’ Then you can get the path to the file as the first element (and only) … Read more

Lua os.execute return value

You can use io.popen() instead. This returns a file handle you can use to read the output of the command. Something like the following may work: local handle = io.popen(command) local result = handle:read(“*a”) handle:close() Note that this will include the trailing newline (if any) that the command emits.

Why is LuaJIT so good?

Mike Pall has talked about this in a few places: http://article.gmane.org/gmane.comp.lang.lua.general/58908 http://lambda-the-ultimate.org/node/3851 http://www.reddit.com/user/mikemike As with every performant system, the answer in the end comes down to two things: algorithms and engineering. LuaJIT uses advanced compilation techniques, and it also has a very finely engineered implementation. For example, when the fancy compilation techniques can’t handle a … Read more

Lua pattern matching vs. regular expressions

Are any common samples where lua pattern matching is “better” compared to regular expression? It is not so much particular examples as that Lua patterns have a higher signal-to-noise ratio than POSIX regular expressions. It is the overall design that is often preferable, not particular examples. Here are some factors that contribute to the good … Read more

How do you copy a Lua table by value?

Table copy has many potential definitions. It depends on whether you want simple or deep copy, whether you want to copy, share or ignore metatables, etc. There is no single implementation that could satisfy everybody. One approach is to simply create a new table and duplicate all key/value pairs: function table.shallow_copy(t) local t2 = {} … Read more

Search for an item in a Lua list

You could use something like a set from Programming in Lua: function Set (list) local set = {} for _, l in ipairs(list) do set[l] = true end return set end Then you could put your list in the Set and test for membership: local items = Set { “apple”, “orange”, “pear”, “banana” } if … Read more