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?
well
Started by SpencerBeige, Dec 14 2014 03:21 AM
9 replies to this topic
#1
Posted 14 December 2014 - 03:21 AM
#2
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.
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.
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.
#3
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
endYou could also do this:local file = fs.open("filename", "r")
repeat
line = file.readLine()
--# stuff
until not line
#4
Posted 14 December 2014 - 05:07 AM
The loop for the second one should really be:
Otherwise, the loop would iterate with line being nil the last time.
local line = file.readLine() while line do --# stuff line = file.readLine() end
Otherwise, the loop would iterate with line being nil the last time.
#5
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.
#6
Posted 14 December 2014 - 05:37 AM
Lyqyd, on 14 December 2014 - 05:07 AM, said:
The loop for the second one should really be:
Otherwise, the loop would iterate with line being nil the last time.
local line = file.readLine() while line do --# stuff line = file.readLine() end
Otherwise, the loop would iterate with line being nil the last time.
EDIT: Ninja'd.
Edited by AgentE382, 14 December 2014 - 05:38 AM.
#7
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
#8
Posted 14 December 2014 - 02:02 PM
thx guys
#9
Posted 14 December 2014 - 04:15 PM
Lyqyd, on 14 December 2014 - 05:07 AM, said:
The loop for the second one should really be:
local line = file.readLine() while line do --# stuff line = file.readLine() endOtherwise, the loop would iterate with line being nil the last time.
#10
Posted 14 December 2014 - 04:26 PM
slow-coder, on 14 December 2014 - 04:15 PM, said:
Lyqyd, on 14 December 2014 - 05:07 AM, said:
The loop for the second one should really be:
local line = file.readLine() while line do --# stuff line = file.readLine() endOtherwise, the loop would iterate with line being nil the last time.
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.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











