I am trying to figure out how to implement a switch statement.
I read this forum, but it still doesn't help me on how to make one.
viewtopic.php?t=9711
If anyone has any recommendations, please let me know.
Thank you.
Switch Statement?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- HugoBDesigner
- Party member
- Posts: 403
- Joined: Mon Feb 24, 2014 6:54 pm
- Location: Above the Pocket Dimension
- Contact:
Re: Switch Statement?
You can do this as a simple if/else block, like so:
BUT you can also make it a function, to make things simpler for you (if you're too used to Java or similar):
Didn't work TOO much on this, but should work just fine. Here's how it works:
switch(number,
{case 1, function, arguments (in a table)},
{case 2, function, arguments (in a table)},
{case 3, function, arguments (in a table)},
{function, arguments (in a table)} --default
)
This can be done in many ways (without tables, automatically detecting no arguments or single arguments, etc.), but I just wanted to give a fast example. I'm pretty sure a more experienced coder can do something better. I hope it works for you
Code: Select all
local a = 3
if a == 1 then --case 1
print("a = 1")
elseif a == 2 then --case 2
print("a = 2")
elseif a == 3 then --case 3
print("a = 3")
else --default
print("No other option fits")
end
Code: Select all
local a = 3
switch(a,
{1, print, {"a = 1"}},
{2, print, {"a = 2"}},
{3, print, {"a = 3"}},
{print, {"No other option fits"}})
function switch(...)
local args = {...}
local a = args[1] --Your actual value
for i = 2, #args-1 do --for every "case"
local v = args[i]
if a == v[1] then
v[2](unpack(v[3]))
return
end
end
args[#args][2](args[#args][3]) --"default" thing
end
switch(number,
{case 1, function, arguments (in a table)},
{case 2, function, arguments (in a table)},
{case 3, function, arguments (in a table)},
{function, arguments (in a table)} --default
)
This can be done in many ways (without tables, automatically detecting no arguments or single arguments, etc.), but I just wanted to give a fast example. I'm pretty sure a more experienced coder can do something better. I hope it works for you
Re: Switch Statement?
Personally I've been using just if-elseifs. Up to my understanding there really isn't switch-case in lua, but I suppose if and elseifs should be enough. I do miss it from time to time though, like checking keypress and especially when you have several different keypress doing same thing the fall through would be just using or.
c
vs.
lua
Edit: Changed the escape in c-language to 27 instead of 'escape'.
Edit2: Sometimes I have needed to have a simple from a <-> b thing, not fully related to switch case but something like. Lets say that I would want to use ascii graphics and then for some reason save the asciis in different values and load them back, I have been using something like:
I guess this is more of a special case + if you happen to have invalid values, the program might crash. Also I am not sure if this is exactly better than using if and elseifs. You can also put functions in table and call them, but I suppose the program might just get really unneccessary complex to read with jump tables.
c
Code: Select all
switch( key ) {
case 'q':
case 27: // escape in ascii
quit();
break;
case 'r':
restart();
break;
default:
doSomethingDefault();
break;
}
lua
Code: Select all
if key == 'q' or key == 'escape' then
quit()
elseif key == 'r' then
restart()
else
doSomethingDefault()
end
Edit2: Sometimes I have needed to have a simple from a <-> b thing, not fully related to switch case but something like. Lets say that I would want to use ascii graphics and then for some reason save the asciis in different values and load them back, I have been using something like:
Code: Select all
local convertTable = {
1 = '#',
2 = '@',
'#' = 1,
'@' = 2
}
print( convertTable[1] ) -- prints #
print( convertTable['#'] ) -- prints 1
Re: Switch Statement?
Thank you!
Big help
Big help
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Switch Statement?
Use tables
If you wanted to have a default value you could do:
You could also implement the goto function, though it wont break, after executing it would go back to running the thing that called it
You could do some other neat things like if the function returns false you can execute the next entry in the table (for that you could use the lua function "next") but I wont get into it too much
The advantage of this approach is that you can easily reuse the switch over and over again
Code: Select all
switch = {
["a"] = function () --case "a":
--The code in case of a
end;
["b"] = function() --case "b":
--The code in case of b
end
}
switch["a"]() --The code in case that the switch is a
Code: Select all
function myswitch (a)
if a and switch[a] then
return switch[a]()
else
return switch["defaul"]()
end
end
Code: Select all
function mygoto (a)
return switch[a]()
end
The advantage of this approach is that you can easily reuse the switch over and over again
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: Switch Statement?
Lua-users has a pretty in depth look at lua switch.
http://lua-users.org/wiki/SwitchStatement
You probably don't need anything more then a table and if statements.
http://lua-users.org/wiki/SwitchStatement
You probably don't need anything more then a table and if statements.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 16 guests