Jump to content




tableDump - a simple way to save a table to a file and view it


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

#1 Stekeblad

  • Members
  • 62 posts
  • LocationSweden

Posted 01 December 2016 - 01:48 PM

Have you ever had a table and wanted to easy view it, like one for a peripheral?

Did you try textutils or something like:
for k, v in pairs(table) do print("k .. "  " .. v) end
but it errors because the table contains functions?

If so, this will help you!


tableDump is pretty simple but still very usefull, you call it with two parameters, the first is your table and the second is the name of a file to save the table to.
Strings and numbers are no problem, functions will be replaced by [FUNCTION] in the output and nested tables is no problem!

Are you still thinking that textutils.serialise/textutils.serialize is better?
tableDump also split keys and values into columns instead of just a space or two between them making it easier to read.

What is the downside with this you may think, there is currently not possible to easy convert it back to a table from a file.

IMAGES!
Spoiler

License and download

MIT-license, you are free to use and modify this program
pastebin get DL4cgLZK td
lua
os.loadAPI("td")

Edited by Stekeblad, 03 December 2016 - 01:13 PM.


#2 HaddockDev

  • Members
  • 61 posts
  • LocationSomewhere, deep down under a rainbow.

Posted 02 December 2016 - 03:02 AM

Couldn't you just use textutils, instead of writing some functions? (But I do admit, nice code)
Something like
function save(table, file)
local h = fs.open(file, "w")
h.write(textutils.serialize(table))
h.close()
end

--fyi, i did not make this code really nice and functional, its a "do as i say, and dont complain" kinda thing

function load(file)
local h = fs.open(file,"r")
local r = h.readAll()
h.close()
return textutils.unserialize(r)
end
Not trying to criticise you, but CC already has an API for it. You did do a good job of coding it though.
[offtopic]Now I'm having flashbacks to creating the pkg encryption function, bad times.[/offtopic]

Edited by Haddock, 02 December 2016 - 03:05 AM.


#3 Bomb Bloke

    Hobbyist Coder

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

Posted 02 December 2016 - 03:34 AM

textutils.serialise() is also unable to handle functions.

#4 Stekeblad

  • Members
  • 62 posts
  • LocationSweden

Posted 02 December 2016 - 03:14 PM

I wrote this because textutils cant handle functions inside a table.

#5 HaddockDev

  • Members
  • 61 posts
  • LocationSomewhere, deep down under a rainbow.

Posted 02 December 2016 - 08:22 PM

View PostBomb Bloke, on 02 December 2016 - 03:34 AM, said:

textutils.serialise() is also unable to handle functions.

View PostStekeblad, on 02 December 2016 - 03:14 PM, said:

I wrote this because textutils cant handle functions inside a table.
Oh, sorry for my mistake then. Well, technically it could by using string.dump on a function combined with loadstring, but I'm not exactly sure if that'd work or not.

#6 apemanzilla

  • Members
  • 1,421 posts

Posted 03 December 2016 - 01:24 AM

View PostHaddock, on 02 December 2016 - 08:22 PM, said:

View PostBomb Bloke, on 02 December 2016 - 03:34 AM, said:

textutils.serialise() is also unable to handle functions.

View PostStekeblad, on 02 December 2016 - 03:14 PM, said:

I wrote this because textutils cant handle functions inside a table.
Oh, sorry for my mistake then. Well, technically it could by using string.dump on a function combined with loadstring, but I'm not exactly sure if that'd work or not.

It wouldn't unless you somehow managed to also store the locals and environment.

#7 Stekeblad

  • Members
  • 62 posts
  • LocationSweden

Posted 03 December 2016 - 12:16 PM

If you want the code of the function, it can be found in the program code because functions is normally not changed while the program is running, right?
Some program data is often changed and then things like tableDump can put it in a file for easy reading, the data does not in all cases exist in the code.

Edited by Stekeblad, 03 December 2016 - 01:12 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users