This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
getting the number of lines in a file
Started by ETHANATOR360, 25 March 2013 - 09:55 AMPosted 25 March 2013 - 10:55 AM
how do you get the number of lines in a file
Posted 25 March 2013 - 11:03 AM
local handle = fs.open(fileName,"r") --Open the file in read mode
local lines = handle.readLine()
local data = {} --Define a table
for lines in handle.readLine do
table.insert(data,lines) --Insert data into a table
end
handle.close() --Close the handle
print(#data) --Print the number of lines in the table 'data' which is a copy of every line in fileName
A bonus:This code also copies all the lines in that file :P/>
Posted 25 March 2013 - 11:43 AM
thanks ill mention you when i post the program im working on
Posted 25 March 2013 - 11:54 AM
A bonus:local handle = fs.open(fileName,"r") --Open the file in read mode local lines = handle.readLine() local data = {} --Define a table for lines in handle.readLine do table.insert(data,lines) --Insert data into a table end handle.close() --Close the handle print(#data) --Print the number of lines in the table 'data' which is a copy of every line in fileName
This code also copies all the lines in that file :P/>
I've never seen this particular operation done that way. This is probably because I didn't know that fs.readLine was an iterator function or could be used as one.
It might be a bit faster to do it this way since you don't have to keep going back to the handle to read the next line:
local function getLinesInFile (filePath)
-- Get a handle on the file, read all of it in one call, then close the handle.
local fileHandle = fs.open (filePath, 'r')
local fileContenets = fileHandle.readAll()
fileHandle.close()
-- Use string matching by non endline characters to get all lines in the file contents string.
local linesInFile = {}
for line in string.gmatch (fileContents, "[^\n]+") do
table.insert (linesInFile, line)
end
-- Return all of the lines in the file.
return linesInFile
end
Posted 25 March 2013 - 12:02 PM
better:A bonus:local handle = fs.open(fileName,"r") --Open the file in read mode local lines = handle.readLine() local data = {} --Define a table for lines in handle.readLine do table.insert(data,lines) --Insert data into a table end handle.close() --Close the handle print(#data) --Print the number of lines in the table 'data' which is a copy of every line in fileName
This code also copies all the lines in that file :P/>
local file=io.open("file")
local cnt=0
for line in file:lines() do
cnt=cnt+1
end
return cnt
better:I've never seen this particular operation done that way. This is probably because I didn't know that fs.readLine was an iterator function or could be used as one.
It might be a bit faster to do it this way since you don't have to keep going back to the handle to read the next line:local function getLinesInFile (filePath) -- Get a handle on the file, read all of it in one call, then close the handle. local fileHandle = fs.open (filePath, 'r') local fileContenets = fileHandle.readAll() fileHandle.close() -- Use string matching by non endline characters to get all lines in the file contents string. local linesInFile = {} for line in string.gmatch (fileContents, "[^\n]+") do table.insert (linesInFile, line) end -- Return all of the lines in the file. return linesInFile end
local file=io.open("file")
local cnt={}
for line in file:lines() do
table.insert(cnt,line)
end
return cnt
Posted 25 March 2013 - 12:06 PM
Cool beans. I'll be waiting ^_^/>thanks ill mention you when i post the program im working on
Posted 25 March 2013 - 03:14 PM
Better:local file=io.open("file") local cnt=0 for line in file:lines() do cnt=cnt+1 end return cnt
local h = io.open("file", "r")
local count = 0
for line in h:lines() do
count = count + 1
end
h:close()
return count
Posted 25 March 2013 - 03:22 PM
You both forgot to close the file.
Posted 25 March 2013 - 03:24 PM
whops, I knew there was something else I had to add. but there are people on these forums that could debate that closing a file handle is pointless *cough* ChunLing *cough*You both forgot to close the file.
Posted 25 March 2013 - 03:31 PM
Close ALL the file handles! It could save lives…whops, I knew there was something else I had to add. but there are people on these forums that could debate that closing a file handle is pointless *cough* ChunLing *cough*You both forgot to close the file.
Posted 25 March 2013 - 03:32 PM
Thats a little extreme. and I never said that I'm against it, I said that there are SOME people that areClose ALL the file handles! It could save lives…
Posted 25 March 2013 - 03:35 PM
Pfft <_</>Thats a little extreme.
I never said you were either :P/>I never said that I'm against it
Away with them!I said that there are SOME people that are
Posted 25 March 2013 - 09:41 PM
Err… How is closing a file pointless? You can't delete a file until you've closed all instances of file handles that direct to it…
Posted 26 March 2013 - 01:55 AM
Not really, io:lines closes the file when it reaches the end, so you don't have to do it. It won't error or anything, but it's just pointless.You both forgot to close the file.