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

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

Started by Stekeblad, 01 December 2016 - 12:48 PM
Stekeblad #1
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



Tab Exchanger is not shown as it contains a slightly outdated output


License and download

MIT-license, you are free to use and modify this program

pastebin get DL4cgLZK td
lua
os.loadAPI("td")
Edited on 03 December 2016 - 12:13 PM
HaddockDev #2
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 on 02 December 2016 - 02:05 AM
Bomb Bloke #3
Posted 02 December 2016 - 03:34 AM
textutils.serialise() is also unable to handle functions.
Stekeblad #4
Posted 02 December 2016 - 03:14 PM
I wrote this because textutils cant handle functions inside a table.
HaddockDev #5
Posted 02 December 2016 - 08:22 PM
textutils.serialise() is also unable to handle functions.
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.
apemanzilla #6
Posted 03 December 2016 - 01:24 AM
textutils.serialise() is also unable to handle functions.
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.
Stekeblad #7
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 on 03 December 2016 - 12:12 PM