【问题标题】:Searching for a table in table lua在表 lua 中搜索表
【发布时间】:2017-05-02 15:39:04
【问题描述】:

我有一个表结构如下:

[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

问题是我停留在索引 i,而 v1[i] 是一个“SERVICE”,所以他也直接进入了最后一个 if 子句。如何结束一个循环迭代(在第一个 if 子句之后)。或者有没有更好的方法来做到这一点?

感谢您的建议。 西奥

【问题讨论】:

  • 对一些::label::使用goto

标签: lua lua-table


【解决方案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(其他 ttype 表直到下一个 SERVICE)
SERVICE_2(其他 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 (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

【讨论】:

  • 谢谢。对我来说效果很好。
猜你喜欢
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-22
  • 2021-12-02
  • 2017-04-04
  • 2019-03-01
  • 2012-09-22
相关资源
最近更新 更多