RosettaCodeData/Task/Menu/Lua/menu.lua

19 lines
546 B
Lua
Raw Permalink Normal View History

2020-02-17 23:21:07 -08:00
function select (list)
if not list or #list == 0 then
return ""
end
local last, sel = #list
repeat
for i,option in ipairs(list) do
io.write(i, ". ", option, "\n")
end
io.write("Choose an item (1-", tostring(last), "): ")
sel = tonumber(string.match(io.read("*l"), "^%d+$"))
until type(sel) == "number" and sel >= 1 and sel <= last
return list[math.floor(sel)]
2013-04-10 21:29:02 -07:00
end
2020-02-17 23:21:07 -08:00
print("Nothing:", select {})
print()
print("You chose:", select {"fee fie", "huff and puff", "mirror mirror", "tick tock"})