2016-11-27 76 views
1

我需要存儲表中的文件的所有行,但我需要開始閱讀它在一個特定的點。這裏的文件示例如下:開始從一個特定的行讀取文件

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; 

但(當然)這整個文件循環。我需要從程序循環到結束

回答

0
local inside 
for line in io.lines(file) do 
    inside = inside or line:match"^program" 
    table.insert(program.body, inside and line); 
end; 
+0

您能否向我解釋「內部」變量中發生了什麼?謝謝。 :) –

+0

'inside'在以「program」開頭的行之前是'nil',並且在該行之後不是'nil'。 [「和」在Lua中](https://www.lua.org/pil/3.3.html) –