Guest User

Untitled

a guest
Jan 10th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. -- [[
  2. Equivalent code for
  3.  
  4. if color == "red" then
  5. print("Color is red")
  6. elseif color == "blue" then
  7. print("Color is blue")
  8. elseif color == "green" then
  9. print("Color is green")
  10. end
  11. ]]
  12.  
  13. -- Assemble switch table
  14.  
  15. local switch = {
  16. ["red"] = function() print("Color is red") end,
  17. ["blue"] = function() print("Color is blue") end,
  18. ["green"] = function() print("Color is green") end,
  19. }
  20.  
  21. -- Get the function stored at switch[color], then call it
  22.  
  23. what_to_do = switch[color]
  24. what_to_do()
  25.  
  26. -- Or, the shorter alternative, just do
  27. -- switch[color]()
Advertisement
Add Comment
Please, Sign In to add comment