Jump to content




Writing Tables to Files


  • This topic is locked This topic is locked
3 replies to this topic

#1 cdel

  • Banned
  • 496 posts
  • LocationMelbourne, Australia

Posted 25 September 2014 - 09:25 AM

I am a bit confused on how I would write a table to a file.

An example code setup:

table = { "Hello", "World", "!" }

local file = fs.open("hai", "w")
file.write(table)
file.close()

'hai' file:

{3.0=!, 2.0=World, 1.0=Hello}

How do I make it look like a 'normal' table as it would in another program.

Edited by connordelaneyy, 25 September 2014 - 09:27 AM.


#2 Bomb Bloke

    Hobbyist Coder

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

Posted 25 September 2014 - 10:11 AM

In your example, you create a table and store the pointer that leads to it in your "table" variable. Then you attempt to write that pointer to disk.

Take a look at textutils.serialize() / textutils.unserialize().

Edit:

Hang on, you're saying fs.write() produced "{3.0=!, 2.0=World, 1.0=Hello}" without a specific serialisation call? I honestly wouldn't've expected it to do even that much... In any case, you should get better results with the textutils API.

Edited by Bomb Bloke, 25 September 2014 - 10:16 AM.


#3 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 25 September 2014 - 10:14 AM

Edit: Ninja'd....

You're looking for textutils.serialise, or unserialise if you want to read a table.

Note that serialise (UK spelling) is valid, but only since 1.6. It's up to you whether to use the UK spelling or the US spelling.

local _table = { "Hello", "World", "!" }
local file = fs.open("hai", "w")
file.write(textutils.serialise(_table))
file.close()

Also you shouldn't use table a variable name, it's used for the actual table API/object/whatever you want to call it.
Posted Image

Edited by oeed, 25 September 2014 - 10:15 AM.


#4 cdel

  • Banned
  • 496 posts
  • LocationMelbourne, Australia

Posted 25 September 2014 - 10:19 AM

View PostBomb Bloke, on 25 September 2014 - 10:11 AM, said:

In your example, you create a table and store the pointer that leads to it in your "table" variable. Then you attempt to write that pointer to disk.

Take a look at textutils.serialize() / textutils.unserialize().

Edit:

Hang on, you're saying fs.write() produced "{3.0=!, 2.0=World, 1.0=Hello}" without a specific serialisation call? I honestly wouldn't've expected it to do even that much... In any case, you should get better results with the textutils API.

I didn't expect it either. :unsure:

View Postoeed, on 25 September 2014 - 10:14 AM, said:

Edit: Ninja'd....

You're looking for textutils.serialise, or unserialise if you want to read a table.

Note that serialise (UK spelling) is valid, but only since 1.6. It's up to you whether to use the UK spelling or the US spelling.

local _table = { "Hello", "World", "!" }
local file = fs.open("hai", "w")
file.write(textutils.serialise(_table))
file.close()

Also you shouldn't use table a variable name, it's used for the actual table API/object/whatever you want to call it.
Posted Image

I knew about serialising tables and such, just didn't know if it was different for some reason when writing to files. thanks :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users