Uhh i was wondering if lua had a select function like other languages, i think vbs is something like Select case ()
case "1" and case "2" or something... just wondering.
case "1" and case "2" or something... just wondering.
This forum is a read-only archive of the Gammon Software forum (2000–2026). No new posts can be made. Search the archive.
Posted by Metsuro on Tue 03 Jan 2006 01:01 AM — 14 posts, 63,771 views.
function switch (selector, t)
assert (type (t) == "table",
"Table not supplied to switch")
local f = t [selector]
if f then
assert (type (f) == "function", "Not a function")
f ()
else
print "default"
end -- if
end -- switch
-- test
-- example table of selectors and actions
actions = {
[1] = function () print "action 1" end ,
[2] = function () print "action 2" end ,
[3] = function () print "action 3" end,
["a"] = function () print "action a" end ,
}
-- try to switch ...
switch (1, actions)
switch (3, actions)
switch ("a", actions)
switch (99, actions)
function switch (selector, t)
assert (type (t) == "table", "Table not supplied to switch")
local f = t [selector]
if f then
assert (type (f) == "function", "Not a function")
f ()
else
print "default"
end -- if
end -- switch
-- test
-- example table of selectors and actions
function action_1 ()
print "do something"
print "action 1"
end -- action_1
function action_2_or_3 ()
print "do something"
print "action 2 or 3"
end -- action_2_or_3
function action_a ()
print "this is action 'a'"
end -- action_a
actions = {
[1] = action_1 ,
[2] = action_2_or_3 ,
[3] = action_2_or_3 ,
["a"] = action_a ,
}
-- try to switch ...
switch (1, actions)
switch (3, actions)
switch ("a", actions)
switch (99, actions)
function switch (selector, t, default)
assert (type (t) == "table", "Table not supplied to switch")
local f = t [selector]
if f then
assert (type (f) == "function", "Not a function")
f ()
else
if type (default) == "function" then
default ()
end -- default supplied
end -- if
end -- switch
-- test
-- example table of selectors and actions
function action_1 ()
print "do something"
print "action 1"
end -- action_1
function action_2_or_3 ()
print "do something"
print "action 2 or 3"
end -- action_2_or_3
function action_a ()
print "this is action 'a'"
end -- action_a
actions = {
[1] = action_1 ,
[2] = action_2_or_3 ,
[3] = action_2_or_3 ,
["a"] = action_a ,
}
function default ()
print "default taken here"
end -- default
-- try to switch ...
switch (1, actions, default)
switch (3, actions, default)
switch ("a", actions, default)
switch (99, actions, default)
function switch (selector, t)
assert (type (t) == "table", "Table not supplied to switch")
local f = t [selector]
if f then
assert (type (f) == "function", "Not a function")
f ()
else
f = t [1/0]
if type (f) == "function" then
f ()
end -- default in table
end -- if
end -- switch
-- test
-- example table of selectors and actions
function action_1 ()
print "do something"
print "action 1"
end -- action_1
function action_2_or_3 ()
print "do something"
print "action 2 or 3"
end -- action_2_or_3
function action_a ()
print "this is action 'a'"
end -- action_a
function action_default ()
print "default action"
end -- action_a
actions = {
[1] = action_1 ,
[2] = action_2_or_3 ,
[3] = action_2_or_3 ,
["a"] = action_a ,
[1/0] = action_default
}
-- try to switch ...
switch (1, actions)
switch (3, actions)
switch ("a", actions)
switch (99, actions)
DEFAULT = 1/0 --> special key for default case
function switch (selector, t)
assert (type (t) == "table", "Table not supplied to switch")
local f = t [selector]
if f then
assert (type (f) == "function", "Not a function")
f ()
else
f = t [DEFAULT]
if type (f) == "function" then
f ()
end -- default in table
end -- if
end -- switch
-- test
-- example table of selectors and actions
function action_1 ()
print "do something"
print "action 1"
end -- action_1
function action_2_or_3 ()
print "do something"
print "action 2 or 3"
end -- action_2_or_3
function action_a ()
print "this is action 'a'"
end -- action_a
function action_default ()
print "default action"
end -- action_a
actions = {
[1] = action_1 ,
[2] = action_2_or_3 ,
[3] = action_2_or_3 ,
["a"] = action_a ,
DEFAULT = action_default
}
-- try to switch ...
switch (1, actions)
switch (3, actions)
switch ("a", actions)
switch (99, actions)
function switch (selector, t, default)
assert (type (t) == "table", "Table not supplied to switch")
local f = t [selector]
if f then
assert (type (f) == "function", "Not a function")
f ()
else
if type (default) == "function" then
default ()
end -- default supplied
end -- if
end -- switch
-- test
function myfunc (sel)
switch (sel,
{
[1] = function () print "action 1" end,
[2] = function () print "action 2" end,
[42] = function () print "action 42" end,
},
-- default:
function () print "default taken" end
) -- end of switch
end -- myfunc
myfunc (1)
myfunc (2)
myfunc (42)
myfunc ("blah")
SPELL_FUN *spell_function( char *name )
{
if( !str_cmp( name, "spell_smaug" ) )
return spell_smaug;
if( !str_cmp( name, "spell_acid_blast" ) )
return spell_acid_blast;
if( !str_cmp( name, "spell_animate_dead" ) )
return spell_animate_dead;
if( !str_cmp( name, "spell_astral_walk" ) )
return spell_astral_walk;
... and so on ...
spellnames = {
spell_smaug = spell_smaug,
spell_acid_blast = spell_acid_blast,
spell_animate_dead = spell_animate_dead,
spell_astral_walk = spell_astral_walk,
-- and so on
}
spellnames = {
["spell_smaug"] = spell_smaug,
["spell_acid_blast"] = spell_acid_blast,
["spell_animate_dead"] = spell_animate_dead,
["spell_astral_walk"] = spell_astral_walk,
-- and so on
}
rev_spellnames = {}
for k, v in pairs (spellnames) do
rev_spellnames [v] = k
end
t = { [default] = "private value"}default a local variable to avoid polluting the global namespace, and to make sure that only the current chunk of code can see it.
actions = {
[1] = action_1 ,
[2] = action_2_or_3 ,
[3] = action_2_or_3 ,
[\"a"] = action_a ,
DEFAULT = action_default
}
actions = {
[1] = action_1 ,
[2] = action_2_or_3 ,
[3] = action_2_or_3 ,
["a"] = action_a ,
[DEFAULT] = action_default
}