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

[Lua] Load function from file

Started by exploder, 18 January 2013 - 05:13 AM
exploder #1
Posted 18 January 2013 - 06:13 AM
Hi everyone. I want to make function that can get data from files, but I can't figure it out how to make one work.


So if I would like to load my password from file then I would write
load("password",password,pass)
print(pass)

But the computer will only print out empty space, so how can I fix this?

Code:

function load(name,tablename,data,)
file = fs.open("security/door/"..name,"r")
tablename = {} -- Table should be named password.
local line = file.readLine()
table.insert(tablename,line)
file.close()
data = tablename[1] -- Data should be named pass.
end


Thank you.
remiX #2
Posted 18 January 2013 - 06:19 AM
Why insert into a table, what you could do is return what's in the file

function load(fileName)
	file = fs.open("security/door/"..fileName,"r")
	local line = file.readLine()
	file.close()
	return line or "none" -- will return none if the file is empty
end
--Call it like
password = load("password")
But I do see what you're trying to do… but the above should suffice?
exploder #3
Posted 18 January 2013 - 10:40 AM
Why insert into a table, what you could do is return what's in the file

function load(fileName)
	file = fs.open("security/door/"..fileName,"r")
	local line = file.readLine()
	file.close()
	return line or "none" -- will return none if the file is empty
end
--Call it like
password = load("password")
But I do see what you're trying to do… but the above should suffice?

Works great, thank you.
KaoS #4
Posted 18 January 2013 - 10:45 AM
I also find it useful to note that file.close() returns nil, allowing us to summarise the above into

function load(fileName)
	    file = fs.open("security/door/"..fileName,"r")
	   return (file.readLine() or "none"),file.close()
end
password = load("password")

I know it looks random and not even very useful but I find that many coders use file handles often and this helps keep your coded short
exploder #5
Posted 18 January 2013 - 10:58 AM
I also find it useful to note that file.close() returns nil, allowing us to summarise the above into

function load(fileName)
		file = fs.open("security/door/"..fileName,"r")
	   return (file.readLine() or "none"),file.close()
end
password = load("password")

I know it looks random and not even very useful but I find that many coders use file handles often and this helps keep your coded short

That might come in handy sometimes, thanks.
KaoS #6
Posted 18 January 2013 - 12:39 PM
sure. I think remiX deserved a +1 more than me though lol
remiX #7
Posted 18 January 2013 - 05:29 PM
I also find it useful to note that file.close() returns nil, allowing us to summarise the above into

function load(fileName)
		file = fs.open("security/door/"..fileName,"r")
	   return (file.readLine() or "none"),file.close()
end
password = load("password")

I know it looks random and not even very useful but I find that many coders use file handles often and this helps keep your coded short

Never knew you could close a file handle on the same line as a return :P/> Good to know
ChunLing #8
Posted 18 January 2013 - 08:41 PM
It's not on the same line, it's actually part of the return…though since it just returns nil it has no effect other than to allow you to return the file.readLine() or "none" and then close the file without having to allocate a variable.