lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great KHMan once stated:
> 
> How many other major languages that avoids having a switch 
> construct are there? Lua puts in a utf8 library but not a switch 
> construct?

  Not built in, but ...

	function switch(var,case)
	  if case[var] then
	    case[var]()
	  end
	end
	
	x = 4
	
	switch(x, {
	  one   = function() print "yada"  end,
	  two   = function() print "blah"  end,
	  three = function() print "yahoo" end,
	  [4]   = function() print "this is four" end,
	})

  You still have the overhead of a table lookup, but this is about as close
to a switch statement you can get in Lua without changing the language.

  -spc