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

getting the number of lines in a file

Started by ETHANATOR360, 25 March 2013 - 09:55 AM
ETHANATOR360 #1
Posted 25 March 2013 - 10:55 AM
how do you get the number of lines in a file
SuicidalSTDz #2
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/>
ETHANATOR360 #3
Posted 25 March 2013 - 11:43 AM
thanks ill mention you when i post the program im working on
Grim Reaper #4
Posted 25 March 2013 - 11:54 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/>

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
PixelToast #5
Posted 25 March 2013 - 12:02 PM

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/>
better:

local file=io.open("file")
local cnt=0
for line in file:lines() do
  cnt=cnt+1
end
return cnt

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
better:

local file=io.open("file")
local cnt={}
for line in file:lines() do
  table.insert(cnt,line)
end
return cnt
SuicidalSTDz #6
Posted 25 March 2013 - 12:06 PM
thanks ill mention you when i post the program im working on
Cool beans. I'll be waiting ^_^/>
theoriginalbit #7
Posted 25 March 2013 - 03:14 PM

local file=io.open("file")
local cnt=0
for line in file:lines() do
  cnt=cnt+1
end
return cnt
Better:

local h = io.open("file", "r")
local count = 0
for line in h:lines() do
  count = count + 1
end
h:close()
return count
Kingdaro #8
Posted 25 March 2013 - 03:22 PM
You both forgot to close the file.
theoriginalbit #9
Posted 25 March 2013 - 03:24 PM
You both forgot to close the file.
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*
SuicidalSTDz #10
Posted 25 March 2013 - 03:31 PM
You both forgot to close the file.
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*
Close ALL the file handles! It could save lives…
theoriginalbit #11
Posted 25 March 2013 - 03:32 PM
Close ALL the file handles! It could save lives…
Thats a little extreme. and I never said that I'm against it, I said that there are SOME people that are
SuicidalSTDz #12
Posted 25 March 2013 - 03:35 PM
Thats a little extreme.
Pfft <_</>

I never said that I'm against it
I never said you were either :P/>

I said that there are SOME people that are
Away with them!
Bubba #13
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…
MysticT #14
Posted 26 March 2013 - 01:55 AM
You both forgot to close the file.
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.