【问题标题】:Start reading file from a specific line从特定行开始读取文件
【发布时间】:2017-04-11 10:43:49
【问题描述】:

我需要将文件中的所有行存储在一个表中,但我需要在特定点开始读取它。这是文件示例:

class Foo as
 attribute max : number
 def static show as
 count : number
 begin
 io.print(count)
 return count
 end
 attribute min : number
end
program
 var x : number
 var foo : Foo
 x = 20
 foo = new Foo
 foo.show(x)
end

我需要开始阅读程序并将程序下面的所有内容存储在一个表格中。

我已经这样做了:

for line in io.lines(file) do
    table.insert(program.body, line);
end;

但这(当然)循环遍历整个文件。我需要从 program 循环到 end

【问题讨论】:

    标签: string lua lua-table


    【解决方案1】:
    local inside
    for line in io.lines(file) do
        inside = inside or line:match"^program"
        table.insert(program.body, inside and line);
    end;
    

    【讨论】:

    • 您能否向我解释一下“内部”变量中发生了什么?谢谢。 :)
    • inside 在以“program”开头的行之前是nil,而不是在该行之后的nil"and" in Lua
    猜你喜欢
    • 2016-09-16
    • 1970-01-01
    • 2016-12-18
    • 2021-10-04
    • 1970-01-01
    • 2017-09-13
    • 2019-01-14
    相关资源
    最近更新 更多