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

well

Started by SpencerBeige, 14 December 2014 - 02:21 AM
SpencerBeige #1
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?
KingofGamesYami #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.


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.
AgentE382 #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
end
You could also do this:
local file = fs.open("filename", "r")
repeat
  line = file.readLine()
  --# stuff
until not line
Lyqyd #4
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.
Bomb Bloke #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.
AgentE382 #6
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
MKlegoman357 #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
SpencerBeige #8
Posted 14 December 2014 - 02:02 PM
thx guys :D/>
SpencerBeige #9
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
Exerro #10
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.