2017-05-02 67 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