Page 1 of 1

Switch Statement?

Posted: Sun Jan 25, 2015 1:20 am
by kerperlo
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.

Re: Switch Statement?

Posted: Sun Jan 25, 2015 1:32 am
by HugoBDesigner
You can do this as a simple if/else block, like so:

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
BUT you can also make it a function, to make things simpler for you (if you're too used to Java or similar):

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
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 :awesome:

Re: Switch Statement?

Posted: Sun Jan 25, 2015 1:45 am
by Muris
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

Code: Select all

switch( key ) {
  case 'q':
  case 27: // escape in ascii
     quit();
  break;
  
  case 'r':
    restart();
  break;

  default:
    doSomethingDefault();
  break;
}
vs.
lua

Code: Select all

if key == 'q' or key == 'escape' then
  quit()
elseif key == 'r' then
  restart()
else
  doSomethingDefault()
end
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:

Code: Select all

local convertTable = {
  1 = '#',
  2 = '@',
  '#' = 1,
  '@' = 2
}

print( convertTable[1] ) -- prints #
print( convertTable['#'] ) -- prints 1
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.

Re: Switch Statement?

Posted: Sun Jan 25, 2015 4:18 am
by kerperlo
Thank you!

Big help :D

Re: Switch Statement?

Posted: Sun Jan 25, 2015 4:23 am
by Positive07
Use tables

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
If you wanted to have a default value you could do:

Code: Select all

function myswitch (a)
     if a and switch[a] then
          return switch[a]()
     else
          return switch["defaul"]()
     end
end
You could also implement the goto function, though it wont break, after executing it would go back to running the thing that called it

Code: Select all

function mygoto (a)
     return switch[a]()
end
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

Re: Switch Statement?

Posted: Sun Jan 25, 2015 7:10 am
by adrix89
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.