Concatenation of strings in Lua

As other answers have said, the string concatenation operator in Lua is two dots. Your simple example would be written like this: filename = “checkbook” filename = filename .. “.tmp” However, there is a caveat to be aware of. Since strings in Lua are immutable, each concatenation creates a new string object and copies the … Read more

How to remove a lua table entry by its key?

No, setting the key’s value to nil is the accepted way of removing an item in the hashmap portion of a table. What you’re doing is standard. However, I’d recommend not overriding table.remove() – for the array portion of a table, the default table.remove() functionality includes renumbering the indices, which your override would not do. … Read more

How to dump a table to console?

If the requirement is “quick and dirty” I’ve found this one useful. Because of 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

How to get number of entries in a Lua table?

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