71 posts
Location
Latvia
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.
2088 posts
Location
South 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?
71 posts
Location
Latvia
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.
1548 posts
Location
That 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
71 posts
Location
Latvia
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.
1548 posts
Location
That dark shadow under your bed...
Posted 18 January 2013 - 12:39 PM
sure. I think remiX deserved a +1 more than me though lol
2088 posts
Location
South Africa
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
2005 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.