2017-05-02 63 views
1

我有一个表结构的表像这样:表LUA搜索表

[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> 
    }, 

我编程的简单循环,看起来对于与Af - Ag型的第一个表,过滤信息并想分配剩余的令牌,直到下一个服务开始相应的服务。我的想法看起来像这样:

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]为“服务”,让他直接进入最后如果子句了。如何结束一个循环迭代(在第一个if-子句之后)。或者有没有更好的方法来做到这一点?

谢谢你的建议。 Theo

+1

使用'goto'来某些':: label ::' – tonypdmtr

回答

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(其它表[类型等到明年SERVICE)
SERVICE_2(其它表[类型等到明年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 
+0

谢谢。为我工作很好。 – Theodor