283 posts
Posted 14 December 2014 - 03:21 AM
i need help to save a file into a table. but not knowing how many lines are in the program. so for example, i cant just do a for loop that adds fs.readLine to a table, bcoz what if there are 10 lines?
3057 posts
Location
United States of America
Posted 14 December 2014 - 03:46 AM
Welp, time for trusty string.gmatch! Since this is a pretty simple request, I'll show you the code.
local file = fs.open( "filename", "r" )
local data = file.readAll()
file.close()
local tLines = {}
for line in data:gmatch( "[^\r\n]+" ) do
tLines[ #tLines + 1 ] = line
end
So, breaking it down
1 - open and read the file
2 - define a new table
3 - use gmatch() to iterate through a string, returning results that do not include \r\n (return, newline). This result is then saved in the next available slot in the table.
131 posts
Posted 14 December 2014 - 03:49 AM
You should just be able to do this:
local file = io.open("filename", "r")
for line in file:lines() do
--# stuff
end
You could also do this:
local file = fs.open("filename", "r")
repeat
line = file.readLine()
--# stuff
until not line
8543 posts
Posted 14 December 2014 - 05:07 AM
The loop for the second one should really be:
local line = file.readLine()
while line do
--# stuff
line = file.readLine()
end
Otherwise, the loop would iterate with line being nil the last time.
7083 posts
Location
Tasmania (AU)
Posted 14 December 2014 - 05:36 AM
When saving directly into a table (eg myTable[#myTable+1]), that doesn't really matter - the end result is the same, and the number of attempted reads is the same. Using a while loop just forces you to add an extra line of code.
131 posts
Posted 14 December 2014 - 05:37 AM
The loop for the second one should really be:
local line = file.readLine()
while line do
--# stuff
line = file.readLine()
end
Otherwise, the loop would iterate with line being nil the last time.
True, but it's fine for his application, since explicitly setting an uninitialized table value to nil does nothing of interest unless there's an __newindex metamethod that does funky magic.
EDIT: Ninja'd.
Edited on 14 December 2014 - 04:38 AM
1140 posts
Location
Kaunas, Lithuania
Posted 14 December 2014 - 07:16 AM
I'd like to remember to you all that
file.readLine works as an iterator function:
local file = fs.open("path", "r")
for line in file.readLine do
--// do stuff with the line
end
283 posts
Posted 14 December 2014 - 02:02 PM
thx guys :D/>
283 posts
Posted 14 December 2014 - 04:15 PM
The loop for the second one should really be:
local line = file.readLine() while line do --# stuff line = file.readLine() end
Otherwise, the loop would iterate with line being nil the last time.
this is true, but i'd have to save the first line read to the table ;p
797 posts
Posted 14 December 2014 - 04:26 PM
The loop for the second one should really be:
local line = file.readLine() while line do --# stuff line = file.readLine() end
Otherwise, the loop would iterate with line being nil the last time.
this is true, but i'd have to save the first line read to the table ;p
local line = file.readLine()
while line do
lines[#lines+1] = line
line = file.readLine()
end
This will save the first line, and every other line in the file.