2016-11-27 12 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

"inside"変数で何が起こっているのか教えてください。ありがとう。 :) –

+0

'inside 'は' program'で始まる行の前に 'nil'であり、その行の後には' nil'ではありません。 ["and" in Lua](https://www.lua.org/pil/3.3.html) –

関連する問題