Tips for golfing in Lua

Printing

There are two ways to print in Lua. print adds a newline to the end, while io.write does not.

load

load is useful in many cases:

-- replace a repeated code segment:
load(("…"):gsub("…","…"))()

-- split a string:
s="1,2,3,4,5,6,7,8,9"
load("a={"..s.."}")()

-- iterate over a string
s="abc123"
s:gsub(".",load"…")

Note that load returns a function, rather than just running the code within.

Falsy values

Unlike many other programming languages like Python and C, 0 is not a falsy value in Lua (it is treated as true). The only values considered falsy are the literal false, and nil. nil is the default value for any undeclared variable, which is very flexible in combination with and/or ternaries.

Other tips