Jump to content




getting the number of lines in a file


  • You cannot reply to this topic
13 replies to this topic

#1 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 25 March 2013 - 10:55 AM

how do you get the number of lines in a file

#2 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

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

#3 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 25 March 2013 - 11:43 AM

thanks ill mention you when i post the program im working on

#4 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 25 March 2013 - 11:54 AM

View PostSuicidalSTDz, on 25 March 2013 - 11:03 AM, said:

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


#5 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 25 March 2013 - 12:02 PM

View PostSuicidalSTDz, on 25 March 2013 - 11:03 AM, said:

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

View PostGrim Reaper, on 25 March 2013 - 11:54 AM, said:

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


#6 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 25 March 2013 - 12:06 PM

View PostETHANATOR360, on 25 March 2013 - 11:43 AM, said:

thanks ill mention you when i post the program im working on
Cool beans. I'll be waiting ^_^

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 March 2013 - 03:14 PM

View PostPixelToast, on 25 March 2013 - 12:02 PM, said:

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


#8 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 25 March 2013 - 03:22 PM

You both forgot to close the file.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 March 2013 - 03:24 PM

View PostKingdaro, on 25 March 2013 - 03:22 PM, said:

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*

#10 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 25 March 2013 - 03:31 PM

View PostTheOriginalBIT, on 25 March 2013 - 03:24 PM, said:

View PostKingdaro, on 25 March 2013 - 03:22 PM, said:

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...

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 March 2013 - 03:32 PM

View PostSuicidalSTDz, on 25 March 2013 - 03:31 PM, said:

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

#12 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 25 March 2013 - 03:35 PM

View PostTheOriginalBIT, on 25 March 2013 - 03:32 PM, said:

Thats a little extreme.
Pfft <_<

View PostTheOriginalBIT, on 25 March 2013 - 03:32 PM, said:

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

View PostTheOriginalBIT, on 25 March 2013 - 03:32 PM, said:

I said that there are SOME people that are
Away with them!

#13 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

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...

#14 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 26 March 2013 - 01:55 AM

View PostKingdaro, on 25 March 2013 - 03:22 PM, said:

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users