Without Switch Statements we need to result to long if/then/elseif statements which are sometimes inefficient and create redundancies.
Code: Select all
TestValue = 'Joe'
if string.lower(TestValue) == 'joe' then
Result = 33
elseif string.lower(TestValue) == 'bob' then
Result = 68
elseif string.lower(TestValue) == 'peter' then
Result = 42
elseif string.lower(TestValue) == 'jane' then
Result = 16
else
Result = 0
end
Code: Select all
TestValue = 'Joe'
Switch = {
joe = 33,
bob = 68,
peter = 42,
jane = 16,
}
Result = Switch[string.lower(TestValue)] or 0
Code: Select all
TestValue = 'Joe'
Switch = {
joe = function() return 33 end,
bob = function() return 68 end,
peter = function() return 42 end,
jane = function() return 16 end,
}
local func = Switch[string.lower(TestValue)] or function() return 0 end
Result = func()