This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mibac138's profile picture

[Lua] [Question] files

Started by mibac138, 21 June 2013 - 01:55 PM
mibac138 #1
Posted 21 June 2013 - 03:55 PM
Hello, is it a method to check how much example file has lines?
theoriginalbit #2
Posted 21 June 2013 - 04:19 PM
No, there is no function provided to be able to provide the line count in a file.

You will need to do one of the following two things:

— Load each line of the file into a table, and get the count of the table
— Use pattern matching to count the new line characters in the contents of the file

Any more suggestions, or any more specific help, will require you to be a bit less vague and tell us why you want to count the lines in a file, only then can a better answer be given.
GravityScore #3
Posted 21 June 2013 - 09:20 PM
Adding some code to BIT's suggestions:

Loading into a table, then counting the entries (sort of, not really - just as easy):

local f = io.open("file", "r")
local lines = 0
while f:read("*l") do lines = lines + 1 end
f:close()

print(lines)

Pattern matching:

local f = io.open("file", "r")
local a = f:read("*a")

-- The quickest way I could think of off the top of my head :P/>/>
local _, lines = a:gsub("\n", "")

print(lines)