I am parsing some files, and I want to save each line of the file to a separate table entry.
Saving Lines to Table
Started by cdel, Feb 18 2015 06:05 AM
7 replies to this topic
#1
Posted 18 February 2015 - 06:05 AM
#2
Posted 18 February 2015 - 06:14 AM
To save an entire file to a table:
Is that what you're looking for?
local fTable = {}
local line = 1
f = fs.open("filepath","r")
repeat
fTable[line] = f.readLine()
line = line+1
until fTable[line-1] == nil
f.close()
Is that what you're looking for?
#3
Posted 18 February 2015 - 06:18 AM
HPWebcamAble, on 18 February 2015 - 06:14 AM, said:
To save an entire file to a table:
Is that what you're looking for?
local fTable = {}
local line = 1
f = fs.open("filepath","r")
repeat
fTable[line] = f.readLine()
line = line+1
until fTable[line-1] == nil
f.close()
Is that what you're looking for?
Probably a better way:
local file = fs.open("file","r")
local tbl = {}
local line = file.readLine()
repeat
table.insert(tbl,line)
line = file.readLine()
until line == nil
file.close()
#4
Posted 18 February 2015 - 06:31 AM
Thanks, the main concept was me wanting to parse a response from my webserver then further and parse it via cc, the table, for simplicity.
#5
Posted 18 February 2015 - 08:19 AM
If I may add
local file = http.get(url)
local tbl = {}
for lineNum, lineContent in file.readLine do
tbl[#tbl+1] = lineContent
end
Edited by Anavrins, 18 February 2015 - 08:28 AM.
#6
Posted 18 February 2015 - 09:54 AM
Hmm, I think that might be just:
for lineContent in file.readLine do
#7
Posted 19 February 2015 - 12:04 AM
Dragon53535, on 18 February 2015 - 06:18 AM, said:
Spoiler
Probably. I think both will work just fine, but yours is shorter.
Anavrins, on 18 February 2015 - 08:19 AM, said:
If I may add
local file = http.get(url)
local tbl = {}
for lineNum, lineContent in file.readLine do --#Maybe for lineContent in file.readLine do?
tbl[#tbl+1] = lineContent
end
Didn't know that was a thing. Probably works fine too.
cdel, on 18 February 2015 - 06:31 AM, said:
Thanks, the main concept was me wanting to parse a response from my webserver then further and parse it via cc, the table, for simplicity.
Ill take that as 'one of these kinda solved my problem'
#8
Posted 19 February 2015 - 12:13 AM
yeah, I decided to use the exact code pretty much, so they could browse previously downloaded messages if they haven't got a stable internet connection.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











