在Lua语言中,switch语句的替代方案是什么? 我在C++中有这段代码,我想知道我如何编写一些替换LUA中的转换语句的代码,因为我面临很多问题,我需要使用这个语句。 int choice; do// loop { cout<<"\n >>> The General Menu <<< \n"; cout << endl; cout<< " press (1) to Add "<<endl; cout<< " press (2) to Save "<<endl; cout<< " press (3) to Quit " << endl; cout<< endl; cout<< "Enter your choice please (1/2/3): "; cin>>choice; switch(choice) { case 1: add(); break; case 2: save(); break; default: cout<<" The program has been terminated "<<endl; cout<<" Thank you! \n"; } } while (choice != 3); int选择; 循环 { cout
Lua:在Lua语言中,switch语句的替代方案是什么? 我在C++中有这段代码,我想知道我如何编写一些替换LUA中的转换语句的代码,因为我面临很多问题,我需要使用这个语句。 int choice; do// loop { cout<<"\n >>> The General Menu <<< \n"; cout << endl; cout<< " press (1) to Add "<<endl; cout<< " press (2) to Save "<<endl; cout<< " press (3) to Quit " << endl; cout<< endl; cout<< "Enter your choice please (1/2/3): "; cin>>choice; switch(choice) { case 1: add(); break; case 2: save(); break; default: cout<<" The program has been terminated "<<endl; cout<<" Thank you! \n"; } } while (choice != 3); int选择; 循环 { cout,lua,switch-statement,Lua,Switch Statement,Lua: 一般来说,如果您希望在Lua中使用switch语句,那么您应该做的是构建一个表。对于选择的简单情况,可能是1、2或失败,一个带有几个条件的简单if语句就足够了。对于更复杂的情况,应该使用函数表: local c_tbl = { [1] = add, [2] = save, } local func = c_tbl[choice] if(func) then func() else print " The program has been terminated."
一般来说,如果您希望在Lua中使用switch语句,那么您应该做的是构建一个表。对于
选择的简单情况,可能是1、2或失败,一个带有几个条件的简单if
语句就足够了。对于更复杂的情况,应该使用函数表:
local c_tbl =
{
[1] = add,
[2] = save,
}
local func = c_tbl[choice]
if(func) then
func()
else
print " The program has been terminated."
print " Thank you!";
end
您可以使用词法作用域来允许表中的函数能够访问局部变量,就像代码是内联编写的一样。如果希望将开关作为可调用的函数,可以使用回调功能的一些有趣的功能:
(下面的示例是一个基于变量类型的switch语句,但是您可以将表索引转换为您想要测试它的任何内容。只需将switch函数的return语句更改为nottestfortype(case))
(这本质上是一种惰性的表查找,很像Python的字典功能,但每个元素都是一个函数)
与上面给出的两个答案相比,我不知道这段代码的性能如何,但使用局部变量应该可以使其足够快,以便重复使用。如果您在全局范围内使用开关函数,它可能会成为项目使用的标准函数。下面是另一个使用loadstring()的有趣方法和表格查找
switch = function(cases,args)
if (cases[args] == nil) then return args else return assert(loadstring ('return ' .. cases[args]))() end
end
local case = 2
local result = switch({
[1] = "2^" .. case,
[2] = string.format("2^%i",case),
[3] = tostring(2^case)
},
case
)
print(result) --> 4
使用此方法有点危险,因为loadstring()类似于Python的eval()函数
在LuaWiki提供的示例中,我发现在每种情况下都写“function(x)”很难看。这是一种简洁的方法
“默认”情况是函数的“返回参数”部分。一个以上版本的切换器(不将表格初始化为变量):
试试这个,希望代码是自解释的;-)和
类似于相同的伪代码格式
print("enter your choice : ")
mychoice = io.read()
switch = function (choice)
-- accepts both number as well as string
choice = choice and tonumber(choice) or choice -- returns a number if the choic is a number or string.
-- Define your cases
case =
{
[1] = function ( ) -- case 1 :
print("your choice is Number 1 ") -- code block
end, -- break statement
add = function ( ) -- case 'add' :
print("your choice is string add ") -- code block
end, -- break statement
['+'] = function ( ) -- case '+' :
print("your choice is char + ") -- code block
end, -- break statement
default = function ( ) -- default case
print(" your choice is din't match any of those specified cases")
end, -- u cant exclude end hear :-P
}
-- execution section
if case[choice] then
case[choice]()
else
case["default"]()
end
end
-- Now you can use it as a regular function. Tadaaa..!!
switch(mychoice)
我在使用不同参数的函数时遇到了这个问题,而其他答案不能很好地处理这个问题。
我用匿名函数解决了这个问题
-- call the relevant execution based on its opcode
local instructions = {
[01] = function () self:perform_add(table.unpack(valargs)) end,
[02] = function () self:perform_multiply(table.unpack(valargs)) end,
[03] = function () self:perform_store_input(outputargs[1]) end,
[04] = function () self:perform_output(valargs[1]) end,
[05] = function () self:perform_jnz(table.unpack(valargs)) end,
[06] = function () self:perform_jz(table.unpack(valargs)) end,
[07] = function () self:perform_less_than(table.unpack(valargs)) end,
[08] = function () self:perform_equals(table.unpack(valargs)) end,
[99] = function () self:perform_exit() end,
}
local instr = instructions[opcode]
if (instr) then
instr()
else
print("No instruction for opcode " .. opcode)
end
我使用以下代码:
while true do local tmpswitch1 = exp ; --[[ switch <exp> do ]]
if tmpswitch1 == exp1 then --[[ case <exp1> : ]]
-- do something
break
end ;if tmpswitch1 == exp2 then --[[ case <exp2> : ]]
-- do something
break
end ; --[[ default : ]]
-- do something
break ; end --[[ switch tmpswitch1 ]]
而true do local tmpswitch1=exp;--[[switch do]]
如果tmpswitch1==exp1,则--[[case:]
--做点什么
打破
结束;如果tmpswitch1==exp2,则--[[case:]
--做点什么
打破
结束;-->[默认值:]
--做点什么
中断;结束--[[开关tmpswitch1]]
虽然简单地创建一个以案例为索引、以函数为元素的表可能是最快的方法,但我提出了一个IMO具有更好代码可读性的解决方案:
function switch(element)
local Table = {
["Value"] = element,
["DefaultFunction"] = nil,
["Functions"] = {}
}
Table.case = function(testElement, callback)
Table.Functions[testElement] = callback
return Table
end
Table.default = function(callback)
Table.DefaultFunction = callback
return Table
end
Table.process = function()
local Case = Table.Functions[Table.Value]
if Case then
Case()
elseif Table.DefaultFunction then
Table.DefaultFunction()
end
end
return Table
end
示例用法:
switch(Player:GetName())
.case("Kate", function() print("This player's name rhymes with Fate")end)
.case("Tod", function() print("This player's name rhymes with Cod") end)
.default(function() print("This player's name is not Kate or Tod") end)
.process()
或({add,save})[选择]或bye)()
Thank@Nicol这就是我要找的。我打算用if..then..elseif
语句构建一个表。它现在起作用了。我对这个问题的答案进行了一些测试。我对答案进行了一些测试:我的本地开关函数:平均:100000次重复63毫秒。if,elseif解决方案:22毫秒表选择解决方案离子:58毫秒。关键字再次赢得这一天。如果你将本地功能开关转换为全局开关,结果平均为:62毫秒。
while true do local tmpswitch1 = exp ; --[[ switch <exp> do ]]
if tmpswitch1 == exp1 then --[[ case <exp1> : ]]
-- do something
break
end ;if tmpswitch1 == exp2 then --[[ case <exp2> : ]]
-- do something
break
end ; --[[ default : ]]
-- do something
break ; end --[[ switch tmpswitch1 ]]
function switch(element)
local Table = {
["Value"] = element,
["DefaultFunction"] = nil,
["Functions"] = {}
}
Table.case = function(testElement, callback)
Table.Functions[testElement] = callback
return Table
end
Table.default = function(callback)
Table.DefaultFunction = callback
return Table
end
Table.process = function()
local Case = Table.Functions[Table.Value]
if Case then
Case()
elseif Table.DefaultFunction then
Table.DefaultFunction()
end
end
return Table
end
switch(Player:GetName())
.case("Kate", function() print("This player's name rhymes with Fate")end)
.case("Tod", function() print("This player's name rhymes with Cod") end)
.default(function() print("This player's name is not Kate or Tod") end)
.process()