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

Sim*_*aur 4 lua loops

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
Run Code Online (Sandbox Code Playgroud)

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

Ded*_*tor 6

从 Lua 5.2 开始,你可以

goto label -- This statement goes to the jump-label label
::label:: -- The jump-label label
Run Code Online (Sandbox Code Playgroud)

使用在每个循环后检查的标志变量可以使用连续的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
Run Code Online (Sandbox Code Playgroud)

无论如何,您也可以将循环包装在一个函数中并使用return.

function(...)
    for u in ... do
        for a in ... do
            if ... then
                return
            end
        end
    end
end(...)
Run Code Online (Sandbox Code Playgroud)