在 table lua 中搜索 table

Searching for a table in table lua

我在 table 结构中有一个 table,如下所示:

[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 查找第一个 table,过滤掉该信息并想分配其余的令牌,直到下一个服务开始到相应的服务。我的想法是这样的:

 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]是一个"SERVICE",所以他也直接进入了最后一个if子句。如何结束一个循环迭代(在第一个 if 子句之后)。或者有更好的方法吗?

多谢指教。 西奥

我不确定我是否理解你的总体思路,但这里是关于如何在第一个 "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_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