getting the number of lines in a file
Started by ETHANATOR360, Mar 25 2013 10:55 AM
13 replies to this topic
#1
Posted 25 March 2013 - 10:55 AM
how do you get the number of lines in a file
#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
#3
Posted 25 March 2013 - 11:43 AM
thanks ill mention you when i post the program im working on
#4
Posted 25 March 2013 - 11:54 AM
SuicidalSTDz, 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
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
Posted 25 March 2013 - 12:02 PM
SuicidalSTDz, 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
local file=io.open("file")
local cnt=0
for line in file:lines() do
cnt=cnt+1
end
return cnt
Grim 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:
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
#8
Posted 25 March 2013 - 03:22 PM
You both forgot to close the file.
#10
#12
Posted 25 March 2013 - 03:35 PM
TheOriginalBIT, on 25 March 2013 - 03:32 PM, said:
Thats a little extreme.
TheOriginalBIT, on 25 March 2013 - 03:32 PM, said:
I never said that I'm against it
TheOriginalBIT, on 25 March 2013 - 03:32 PM, said:
I said that there are SOME people that are
#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...
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











