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

saving and retrieving a table from an other file

Started by tpnils, 09 October 2012 - 07:11 PM
tpnils #1
Posted 09 October 2012 - 09:11 PM
Hi, I've been looking around and trying stuff on how to save a table on an other file and retrieving it back in the form of a list. The reason I want so save it on an other file is so when i restart my turtle i won't lose my settings. So the main problem is that the table will get saved as a string as you probebly know. I did like to make clear that I am not an expert and I'm still learning but this is what i have come up with:

function savetable()

for i = 1, #inv do
k = fs.open("saves/inv"..i, "w")
k.writeLine(inv)
k.close()
end
end

As i start the program it will run this ones:

for i = 1, 16 do

k = fs.open("saves/inv"..i, "r")
m = k.readLine()
inv = m
k.close()
end

I have tried to do inv = toNumber(m) but it gave an error attempt to call nil which I don't understand why.
faubiguy #2
Posted 09 October 2012 - 09:14 PM
You can use textutils.serialize(table) to turn a table into a string, and textutils.unserialize(string) to turn it back into a table.
OmegaVest #3
Posted 09 October 2012 - 09:15 PM
Okay, so you have the open and close on the wrong side of the for loop.


function saveTable()
   k = fs.open("saved/inv", "w")
   for i = 1, #inv do
	  k.writeLine(inv[i])
   end
end
k.close

ANd, to answer your error, you got that error because it is tonumber, without capitals. If that fails, print that line, see if there is anything there.


Further, run your load this way. Should be smoother.


function loadTable()
   k = fs.open("saved/inv", "r")
   i = 1
   while true do
      m = k.readLine()
      if m ~= nil then
         inv[i] = tonumber(m)
         print("Adding Inventory ", m, " of slot ", i, ".")
         i = i +1
      else
         break
      end
   end
end
tpnils #4
Posted 09 October 2012 - 09:28 PM
You can use textutils.serialize(table) to turn a table into a string, and textutils.unserialize(string) to turn it back into a table.
oh thanks that makes everything allot easier ;)/>/>


Okay, so you have the open and close on the wrong side of the for loop.


function saveTable()
   k = fs.open("saved/inv", "w")
   for i = 1, #inv do
	  k.writeLine(inv[i])
   end
end
k.close

ANd, to answer your error, you got that error because it is tonumber, without capitals. If that fails, print that line, see if there is anything there.


Further, run your load this way. Should be smoother.


function loadTable()
   k = fs.open("saved/inv", "r")
   i = 1
   while true do
	  m = k.readLine()
	  if m ~= nil then
		 inv[i] = tonumber(m)
		 print("Adding Inventory ", m, " of slot ", i, ".")
		 i = i +1
	  else
		 break
	  end
   end
end

Ah yes but I was having problems with reassembling the table when saving to 1 file so I tried to save every input of the table to an other file and trying to reassemble it that way. I'm just explaining my thought prosses :P/>/>
EDIT: thanks for giving me a working code but i like to create the code my self :)/>/>

thank you for replying so quickly both of you :)/>/>