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

[Help]tables as args/saving tables

Started by Communeguy, 27 April 2016 - 12:38 PM
Communeguy #1
Posted 27 April 2016 - 02:38 PM
Hi there. I'm trying to create a function that would save all the tables argued to it (and do some other silly extravagances left unsaid for the purposes of this exercise). What I am envisioning is a function you would argue a table full of table-names to. It would then string each of those tables and save them to separate files. I intend to use this function frequently through the program I'm defining it for to save the program's active database to files for some stability during operation - basically any time the tables are changed.

I've gone as far as defining a table full of the other tables, and I can make the code walk through those tables and deserialize them 1 by 1, but I can't figure out how to also use those tables NAMES for the filename argument. Am I attacking the problem wrong?
KingofGamesYami #2
Posted 27 April 2016 - 03:15 PM
Yes. The function will not have access to your table unless you give it your table. One way or another, you'll have to pass the table and the table's name.
Communeguy #3
Posted 27 April 2016 - 03:44 PM
Alright. This function might not be as easy to generalize as I thought. I'll just go the long way around and save each file manually.
Bomb Bloke #4
Posted 27 April 2016 - 03:45 PM
For eg, if you were doing something like this:

local doStuffWithTables(tables)
	for i = 1, #tables do
		--# process tables[i]
	end
end

local myTables = {table1, table2, table3}

doStuffWithTables(myTables)

… then you might change it to something like this:

local doStuffWithTables(tables)
	for keyname, curTable in pairs(tables) do
		--# Serialise curTable and save it as keyname
	end
end

local myTables = {["table1"] = table1, ["table2"] = table2, ["table3"] = table3}

doStuffWithTables(myTables)
Communeguy #5
Posted 27 April 2016 - 03:59 PM
Oh, that's quite elegant. Thank you.