I wanted to use a switch() case: in one of my script and I couldn't find anywhere how to use it in lua.
After some research, I managed to get it working and I would like to share this in case someone looks for it.
function switch(n, ...)
for _,v in ipairs {...} do
if v[1] == n or v[1] == nil then
return v[2]()
end
end
end
function case(n,f)
return {n,f}
end
function default(f)
return {nil,f}
end
switch(var,
case(1,
--do something
end),
case(2,
--do something
end),
default(
--do something
end)
)