Files and Streams
Line Count
Model file lines with an in-memory string and count each logical line.
in-memory text
For static replay, use fixed text strings instead of reading a real file.
Line Count
line_count.lua
Replay: real traced execution (multi-file project)
local raw = "red|blue"
local text = string.gsub(raw, "|", "\n")
local count = 0
local lastLine = ""
for line in string.gmatch(text, "[^\n]+") do
count = count + 1
lastLine = line
end
print("raw=" .. raw)
print("count=" .. count)
print("lastLine=" .. lastLine)
local raw = "one|two|three"
local text = string.gsub(raw, "|", "\n")
local count = 0
local lastLine = ""
for line in string.gmatch(text, "[^\n]+") do
count = count + 1
lastLine = line
end
print("raw=" .. raw)
print("count=" .. count)
print("lastLine=" .. lastLine)
local raw = "solo"
local text = string.gsub(raw, "|", "\n")
local count = 0
local lastLine = ""
for line in string.gmatch(text, "[^\n]+") do
count = count + 1
lastLine = line
end
print("raw=" .. raw)
print("count=" .. count)
print("lastLine=" .. lastLine)
raw ← red|blue, text ← red blue, count ← 0, lastLine ← (empty)
1local raw→ red|blue = "red|blue" --@raw="one|two|three", "solo"2local text→ red blue = string.gsub(rawred|blue, "|", "\n")3local count→ 0 = 04local lastLine→ (empty) = ""count ← 1, lastLine ← red
pass 1 of 26for linered in string.gmatch(textred blue, "[^\n]+") do7 count→ 1 = count + 18 lastLine→ red = linered9endcount ← 2, lastLine ← blue
pass 2 of 26for lineblue in string.gmatch(textred blue, "[^\n]+") do7 count→ 2 = count + 18 lastLine→ blue = lineblue9endprint("raw=" .. raw)
11print("raw=" .. rawred|blue)12print("count=" .. count2)13print("lastLine=" .. lastLineblue)outputraw=red|blue count=2 lastLine=blue
raw ← one|two|three, text ← one two three, count ← 0, lastLine ← (empty)
1local raw→ one|two|three = "one|two|three"2local text→ one two three = string.gsub(rawone|two|three, "|", "\n")3local count→ 0 = 04local lastLine→ (empty) = ""count ← 1, lastLine ← one
pass 1 of 36for lineone in string.gmatch(textone two three, "[^\n]+") do7 count→ 1 = count + 18 lastLine→ one = lineone9endAll 3 passes — pass 1 is the card above pass linecountlastLine1 one 0 → 1 one 2 two 1 → 2 two 3 three 2 → 3 three print("raw=" .. raw)
11print("raw=" .. rawone|two|three)12print("count=" .. count3)13print("lastLine=" .. lastLinethree)outputraw=one|two|three count=3 lastLine=three
raw ← solo, text ← solo, count ← 0, lastLine ← (empty)
1local raw→ solo = "solo"2local text→ solo = string.gsub(rawsolo, "|", "\n")3local count→ 0 = 04local lastLine→ (empty) = ""count ← 1, lastLine ← solo
6for linesolo in string.gmatch(textsolo, "[^\n]+") do7 count→ 1 = count + 18 lastLine→ solo = linesolo9endprint("raw=" .. raw)
11print("raw=" .. rawsolo)12print("count=" .. count1)13print("lastLine=" .. lastLinesolo)outputraw=solo count=1 lastLine=solo