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

[Lua][Solved]Little help with a function

Started by BlackMenthol, 07 April 2013 - 05:36 AM
BlackMenthol #1
Posted 07 April 2013 - 07:36 AM
Hello :P

So.. I'm writing a script that needs to save different tables on a file and read from them and i'm still noob with coding, so i need a little help.

This is not all the script, it's only the part i have problems with, anyway i wrote this:


inveData = {}
function readfile()
   h = fs.open("id.txt","r")
   unser = h.readAll()
   inveData = textutils.unserialize(unser)
   h.close()
end
readfile()
for k,v in pairs(invedata) do
   print(k.." "..v)
end
and it works fine. In the file i got a serialized table for testing porpuse, and it prints it like it should do.

I wanted to make the function more generic though, to be able to use it with other files and tables so I wrote this:


inveData = {}
function readfile(filez,tavola)
  h = fs.open(filez,"r")
  unser = h.readAll()
  tavola = textutils.unserialize(unser)
  h.close()
end
readfile("id.txt",inveData)
for k,v in pairs(invedata) do
  print(k.." "..v)
end

And this doesn't work, it's gonna print an empty table.

Anyone can explain me why? I know it's something related to the "tavola" argument I added.

Thank you :D
Telokis #2
Posted 07 April 2013 - 07:43 AM
Hello !
You can't give arguments and modify the real value like this.
You should do something like:


function readfile(filez)
  local tavola = {}
  h = fs.open(filez,"r")
  unser = h.readAll()
  tavola = textutils.unserialize(unser)
  h.close()
  return tavola
end
inveData = readfile("id.txt")
for k,v in pairs(invedata) do print(k.." "..v) end

When you call a function, arguments are copies, not references ! ;)/>
Engineer #3
Posted 07 April 2013 - 07:50 AM
Before continueuing, please put your code within [.code] [./code] tags without the dots.

Well, you need to check if the file even exists, and make sure you have a serialized table in the file.

Now I got ninja'd, I still post my function:

local readTable = function( file )
	if not fs.exists( file ) then return false end
	
	local handle = fs.open( file, "r" )
	local reader = handle.readAll()
	handle.close()
	
	return textutils.unserialize( reader )
end

local tableValue = readTable( "path/to/file/here" )
if tableValue then
	for key, value in pairs( tableValue ) do
		print( key .. " " .. value )
	end
else
	print( "File is incorrect, and if you know it is there, make sure that it contains a serialized table" )
end

Be more specific if it doesnt work.
Telokis #4
Posted 07 April 2013 - 08:01 AM
Now I got ninja'd, I still post my function:

I'm sorry… :(/>
BlackMenthol #5
Posted 07 April 2013 - 08:11 AM
Hello !
You can't give arguments and modify the real value like this.
You should do something like:


function readfile(filez)
  local tavola = {}
  h = fs.open(filez,"r")
  unser = h.readAll()
  tavola = textutils.unserialize(unser)
  h.close()
  return tavola
end
inveData = readfile("id.txt")
for k,v in pairs(invedata) do print(k.." "..v) end

When you call a function, arguments are copies, not references ! ;)/>

yay Thanks, this is what I was looking for :P/>

Well, you need to check if the file even exists, and make sure you have a serialized table in the file.

Yes, i know, but I don't check in the script because i'm working with files and tables that I know they already exist.
Thanks for the reply, even if you got ninja'd :P/>