2017-05-02 4 views
1

이 같은 테이블 구조의 테이블이 있습니다테이블 루아에서 테이블을 검색

[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 

문제는 내가 색인 난에 붙어 및 V1 [내가] "서비스"는 것을, 그래서 그는 너무, 바로 마지막 경우 절을 입력 : 내 생각은 같다. 첫 번째 if 절 이후에 루프 반복을 어떻게 끝내야합니까? 아니면 이것을 할 수있는 더 좋은 방법이 있습니까?

감사드립니다. Theo

+1

일부 ':: label ::'에 'goto' 사용 – 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 세번째 0으로 될 if, 첫 번째 iftrue 수 있습니다.

당신의 생각과 같은 벡터의 일종 구축하는 경우 :이 경우
SERVICE_1 (다음 서비스까지 다른 ttype 테이블)
SERVICE_2 (다음 서비스까지 다른 ttype 테이블)
...

을 코드는 다음과 같습니다 :

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