Jump to content




[Lua] Load function from file


  • You cannot reply to this topic
7 replies to this topic

#1 exploder

  • Members
  • 69 posts
  • LocationLatvia

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.

#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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?

#3 exploder

  • Members
  • 69 posts
  • LocationLatvia

Posted 18 January 2013 - 10:40 AM

View PostremiX, on 18 January 2013 - 06:19 AM, said:

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.

#4 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

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

#5 exploder

  • Members
  • 69 posts
  • LocationLatvia

Posted 18 January 2013 - 10:58 AM

View PostKaoS, on 18 January 2013 - 10:45 AM, said:

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.

#6 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 18 January 2013 - 12:39 PM

sure. I think remiX deserved a +1 more than me though lol

#7 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 18 January 2013 - 05:29 PM

View PostKaoS, on 18 January 2013 - 10:45 AM, said:

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

#8 ChunLing

  • Members
  • 2,027 posts

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users