i was just wondering if this :
function verify(filename)
local f = io.open(filename, "r")
if f then
f:close()
return "true"
else
return "false"
end
end
is the best and fastest way and most efficient to check if a file exists or not?
sounds about right,
but returning the strings "true" and "false" is confusing and unnecessary overhead.
use nil (or false) and 1 (or true).
that said, you can skip the whole 'else' part. reaching the function body end, you'll return nil anyway.
QuoteOriginally posted by Sedulus
sounds about right,
but returning the strings "true" and "false" is confusing and unnecessary overhead.
use nil (or false) and 1 (or true).
that said, you can skip the whole 'else' part. reaching the function body end, you'll return nil anyway.
yaps , i agree, is this :
function verify(filename)
local f = io.open(filename, "r")
if f then
f:close()
return "true"
end
end
better?
almost...string.gsub( yourCode, '"true"', 'true' )
almost...
string.gsub( yourCode, '"true"', 'true' )
hmm, i c :
function verify(filename)
local f = io.open(filename, "r")
if f then
f:close()
return true
end
end
Done !!(hopefully)
Sedulus,... I think that returning true / false is one of the best ways around (note btw that they are reserved keywords in Lua so they aren't getting in the way of variables.) The expressions in 'if (expr) then' are evaluated to true or false nil is translated to false too. But I think that it all comes down to how each one of us is used to writing his/her scripts ...
QuoteOriginally posted by Herodes
Sedulus,... I think that returning true / false is one of the best ways around (note btw that they are reserved keywords in Lua so they aren't getting in the way of variables.) The expressions in 'if (expr) then' are evaluated to true or false nil is translated to false too. But I think that it all comes down to how each one of us is used to writing his/her scripts ...
hmm, indeed i agree with that too, but the point is, how to find if a file exists using the least amount of code, and doing that efficiently !!
QuoteOriginally posted by Dessamator
... how to find if a file exists using the least amount of code, and doing that efficiently !!
Your last piece is the way I'd do it myself too.. but I haven't gone into the guts of the io lib functions of Lua.. so I couldn't say for sure what is the bestestestest way to do it. (This one will work just fine too ;)
QuoteOriginally posted by Herodes
QuoteOriginally posted by Dessamator
... how to find if a file exists using the least amount of code, and doing that efficiently !!
Your last piece is the way I'd do it myself too.. but I haven't gone into the guts of the io lib functions of Lua.. so I couldn't say for sure what is the bestestestest way to do it. (This one will work just fine too ;)
well, we'll just have to settle for the next "bestestestest" thing, :D