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

我编写了一个简单的循环来查找具有 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 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 分支之后)?或者有没有更好的方法来做到这一点?

谢谢。 Theo

点赞
用户7900162
用户7900162

我不确定我是否理解了你的总体想法,但是以下是如何在第一个 "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
                --将所需数据存储在一个对象中
                found_service = 1
            else
                if (found_service == 1 and v1[i].ttype ~= "SERVICE") then
                    -- ->将其余部分分配给最后找到的服务
                end
                if found_service == 1 and v1[i].ttype == "SERVICE" then
                    -- ->找到下一个服务-->重新开始
                    found_service = 0
                end
             end
        end
   end
 end

但是我仍然不明白在当前记录不是 "SERVICE" 和 found_service == 0 时应该做什么。另外,在第三个 if 中当 found_service 变为 0 后,第一个 if 再次可能为 true

如果你的想法是构建一些类似于:

SERVICE_1(直到下一个 SERVICE 的其他 ttype 表)

SERVICE_2(直到下一个 SERVICE 的其他 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
                --将所需数据存储在一个对象中
                found_service = 1
                current_service = v1[i]
            else
                if (found_service == 1 and v1[i].ttype ~= "SERVICE") then
                    -- ->将其余部分分配给最后找到的服务
                end
                if found_service == 1 and v1[i].ttype == "SERVICE" then
                    -- ->找到下一个服务-->重新开始
                    current_service = v1[i]
                end
             end
        end
   end
 end
2017-05-02 19:18:32