Jump to content




Saving Lines to Table


  • You cannot reply to this topic
7 replies to this topic

#1 cdel

  • Banned
  • 496 posts
  • LocationMelbourne, Australia

Posted 18 February 2015 - 06:05 AM

I am parsing some files, and I want to save each line of the file to a separate table entry.

#2 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 18 February 2015 - 06:14 AM

To save an entire file to a table:

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 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 18 February 2015 - 06:18 AM

View PostHPWebcamAble, on 18 February 2015 - 06:14 AM, said:

To save an entire file to a table:

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 cdel

  • Banned
  • 496 posts
  • LocationMelbourne, Australia

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 Anavrins

  • Members
  • 775 posts

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 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 18 February 2015 - 09:54 AM

Hmm, I think that might be just:

for lineContent in file.readLine do


#7 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 19 February 2015 - 12:04 AM

View PostDragon53535, on 18 February 2015 - 06:18 AM, said:

Spoiler

Probably. I think both will work just fine, but yours is shorter.

View PostAnavrins, 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.

View Postcdel, 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 cdel

  • Banned
  • 496 posts
  • LocationMelbourne, Australia

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