gpt4 book ai didi

loops - 如果在lua中执行了inner'for'循环,如何跳出外循环

转载 作者:行者123 更新时间:2023-12-04 11:53:32 25 4
gpt4 key购买 nike

S = 'MSH, mdmOBX, TXA'
for u in string.gmatch(S, "([^,%s]+)"),1 do
l[k] = u
for a in string.gmatch(u,'.-([A-Z].*)'),1 do
g[z] = a
print(g)
_G["Map"..l[k]](Msg[g[z]],R[1])
end
_G["Map"..l[k]](Msg[l[k]],R[1])
end

我有上面的代码,我希望只有在不执行内部 for 循环时才执行内部循环之外的语句,我尝试使用 'break' 关键字,但没有用,并且控制权被传递给了外部循环。如何解决这个问题?

最佳答案

从 Lua 5.2 开始,你可以

goto label -- This statement goes to the jump-label label
::label:: -- The jump-label label

使用在每个循环后检查的标志变量,可以使用连续的 break 将其设置为早期救助。 -语句是那些害怕的人的经典 goto ,或因缺乏它而受到谴责。
local earlyreturn
for u in ... do
for a in ... do
if ... then
earlyreturn = true
break
end
end
if earlyreturn then
break
end
end

无论如何,您也可以将循环包装在一个函数中并使用 return .
function(...)
for u in ... do
for a in ... do
if ... then
return
end
end
end
end(...)

关于loops - 如果在lua中执行了inner'for'循环,如何跳出外循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23090836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com