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

table.unserialize(table) not working

Started by enzokarate, 13 March 2013 - 10:47 PM
enzokarate #1
Posted 13 March 2013 - 11:47 PM
Here is my code:

  function readFile(name)
  local f = fs.open(name,"r")
local data = f.readAll()
f.close()
return textutils.unserialize(data)
end
function editFile(fileName,string,line)
if fileName == nil or string == nil then
  error("String expected got nil")
elseif fileName ~= tostring(fileName) or string ~= tostring(string) then
  error("String expected as file name got number")
else
data ={}
  data = readFile(fileName)
  if line == nil then
   table.insert(data, string)
  elseif line ~= tonumber(line) then
   error("Number expected as line, got string.")
  else
   table.insert(data, line, string)
  end
  print(data)
end
end

I run the function and it all works, but i get a string instead of a table, so i cannot add my var string to it using table.insert()
Edited by
Lyqyd #2
Posted 14 March 2013 - 05:53 AM
Split into new topic.

Please paste the file you are trying to edit with this script.
JokerRH #3
Posted 14 March 2013 - 06:07 AM
As Lyqyd already said, give us more information concerning the file you open.
Apart from your title, you could clean up your programm a little:

if fileName ~= tostring(fileName) then
--Could be
if type(fileName) ~= "string" then

if fileName == nil then
--Could be
if not fileName then --nil will be interpretated as false, anything else as true

if line ~= tonumber(line)
--Could be
if type(line) ~= "number" then
And finally: I wouldn't recommend using string as name for a string variable. I don't know if it is used by lua, but since notepad++ and the [.code] script will color it different I'm guessing it is. :P/>

I hope this might help you as well… :D/>
Lyqyd #4
Posted 14 March 2013 - 06:12 AM
Yes, the variable name "string" is used by the string library.