我有一个像这样的表结构中的表:
[1] = {
[1] = {
category = "WORD",
cursor = <filtered>,
ptype = "STATEMENT",
ttype = "SERVICE",
value = "service",
<metatable> = <filtered>
},
[2] = {
category = "VARIABLE",
cursor = <filtered>,
ptype = "STATEMENT",
ttype = "IDENTIFIER",
value = "TestService",
<metatable> = <filtered>
},
[3] = {
ttype = "BRACE_BLOCK",
value = {
[1] = { ...
...
[2] = {
[1] = {
category = "WORD",
cursor = <filtered>,
ptype = "STATEMENT",
ttype = "SERVICE",
value = "service",
<metatable> = <filtered>
},
[2] = {
category = "VARIABLE",
cursor = <filtered>,
ptype = "STATEMENT",
ttype = "IDENTIFIER",
value = "HelloWorld",
<metatable> = <filtered>
},
我编写了一个简单的循环,它查找带有ttype的第一个表,过滤掉该信息并希望分配其余的标记,直到下一个服务开始相应的服务。我的想法看起来像那样:
local found_service = 0
if found_service == 0 then
for k1, v1 in pairs (parse_tree) do
for i=1,#v1 do
if v1[i].ttype == "SERVICE" then
--Store wanted data in an object
found_service = 1
end
if (found_service == 1 and v1[i].ttype ~= "SERVICE") then
-- ->assign the rest to last found service
end
if found_service == 1 and v1[i].ttype == "SERVICE" then
-- ->Found the next service -->starting over
found_service = 0
end
end
end
end
问题是我坚持索引i,而v1 [i]是&#34; SERVICE&#34;,所以他也直接输入了最后一个if子句。如何结束一次循环迭代(在第一个if子句之后)。或者有更好的方法吗?
谢谢你的建议。 西奥
答案 0 :(得分:1)
我不确定我是否理解你的一般想法,但这里是如何在第一次"SERVICE"
捕获事件中跳过循环体的答案。
local found_service = 0
if found_service == 0 then
for k1, v1 in pairs (parse_tree) do
for i=1,#v1 do
if (found_service == 0 and v1[i].ttype == "SERVICE") then
--Store wanted data in an object
found_service = 1
else
if (found_service == 1 and v1[i].ttype ~= "SERVICE") then
-- ->assign the rest to last found service
end
if found_service == 1 and v1[i].ttype == "SERVICE" then
-- ->Found the next service -->starting over
found_service = 0
end
end
end
end
end
但我仍然不明白应该对当前记录做什么,而不是"SERVICE"
和found_service == 0
。顺便说一下,在found_service
在第三个if
中变为0之后的答案中,第一个if
可能再次为true
。
如果您的想法是构建某种类型的向量,例如:
SERVICE_1(其他ttype表,直到下一个SERVICE)
SERVICE_2(其他ttype表,直到下一个SERVICE)
......
在这种情况下,代码可以是:
local found_service = 0
if found_service == 0 then
for k1, v1 in pairs (parse_tree) do
for i=1,#v1 do
if (found_service == 0 and v1[i].ttype == "SERVICE") then
--Store wanted data in an object
found_service = 1
current_service = v1[i]
else
if (found_service == 1 and v1[i].ttype ~= "SERVICE") then
-- ->assign the rest to last found service
end
if found_service == 1 and v1[i].ttype == "SERVICE" then
-- ->Found the next service -->starting over
current_service = v1[i]
end
end
end
end
end