我有下面这段代码:
for k, v in pairs(temptable) do
if string.match(k,'doe') then
if v["name"] == var2 then
txterr = "Invalid name for "..k
duplicate = true
end
if duplicate then
break
end
end
end
当duplicate设置为true时,我想完全退出for循环。目前,即使它找到匹配项,它仍然会遍历表中的所有值。
我尝试使用break语句,但我认为它正在跳出“if”语句。
我考虑了一个do while循环,可以将其包装在整个for循环周围,但我仍需要一种方法来退出for循环。
谢谢。
break
的方式是正确的,所以问题不在代码的这部分。请提供更多上下文信息(例如temptable
的内容,是否在其他地方定义了duplicate
等)。 - catwellif v["name"] == var2 then
中添加一个print "duplicate found"
,并在if duplicate then
中添加另一个print "duplicate is true"
,看看它们是否被触发?您能否也发布temptable
,以便我们可以查看其包含的内容? - Nope