Functions and reusable text
LESSON 07
Normalize an answer once so a player's spacing and capitalization do not change the puzzle.
local Script = {}local function tidy(text)
return (text:match("^%s*(.-)%s*$"))
endfunction Script.normalize(text)
return string.lower(tidy(tostring(text or "")))
endfunction Script.run(context)
local answer = Script.normalize(" WeSt ")
local label = string.format("Dial %02d / %s", 3, answer)
return label
endreturn ScriptExpected result: Dial 03 / west. The local function remains private. normalize is on the returned
table, so another PCA record can call it by name. The ^ and $ pattern anchors mark the ends, %s
matches whitespace, and the capture (.-) takes the shortest middle that fits.
Use text:sub(1, 4) for a slice, text:find(".", 1, true) for a literal dot, and text:gsub("bell", "chime") for
replacement. gsub returns the new string and a replacement count. Strings are immutable. Lua
string indexes count bytes. Use utf8.len for the number of UTF-8 code points and PCA's Lower or
Upper for engine-side text handling.
