switch(table, value [, ...])

Lua does not provide a switch or case statement, therefore nzLua provides an alternative using a switch function. The first argument to switch is a lookup table that contains a set of functions. The second argument selects which function to call. All additional arguments will be passed to the function that matches the lookup value.

The first argument passed to the functions in the lookup table will always be the value that was used to look up the function. The rest of the arguments will be passed as they were provided to the switch statement. If the lookup value is not found in the table, the switch function calls the default function.

Example

ops={}
ops['+'] = function(op,a,b) return a+b end
ops['-'] = function(op,a,b) return a-b end
ops['*'] = function(op,a,b) return a*b end
ops.default = function(op,a,b)
    error( 'Invalid operation: ' || op, 0 )
end

result = switch(ops,'*',6,7)