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

raw
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)
  1. 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) = ""
  2. count ← 1, lastLine ← red

    pass 1 of 2
    6for linered in string.gmatch(textred
    blue, "[^\n]+") do7  count→ 1 = count + 18  lastLine→ red = linered9end
  3. count ← 2, lastLine ← blue

    pass 2 of 2
    6for lineblue in string.gmatch(textred
    blue, "[^\n]+") do7  count→ 2 = count + 18  lastLine→ blue = lineblue9end
  4. print("raw=" .. raw)

    11print("raw=" .. rawred|blue)12print("count=" .. count2)13print("lastLine=" .. lastLineblue)
    outputraw=red|blue
    count=2
    lastLine=blue
  1. 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) = ""
  2. count ← 1, lastLine ← one

    pass 1 of 3
    6for lineone in string.gmatch(textone
    two
    three, "[^\n]+") do7  count→ 1 = count + 18  lastLine→ one = lineone9end
    All 3 passes — pass 1 is the card above
    passlinecountlastLine
    1one0 1one
    2two1 2two
    3three2 3three
  3. print("raw=" .. raw)

    11print("raw=" .. rawone|two|three)12print("count=" .. count3)13print("lastLine=" .. lastLinethree)
    outputraw=one|two|three
    count=3
    lastLine=three
  1. 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) = ""
  2. count ← 1, lastLine ← solo

    6for linesolo in string.gmatch(textsolo, "[^\n]+") do7  count→ 1 = count + 18  lastLine→ solo = linesolo9end
  3. print("raw=" .. raw)

    11print("raw=" .. rawsolo)12print("count=" .. count1)13print("lastLine=" .. lastLinesolo)
    outputraw=solo
    count=1
    lastLine=solo