PtokaX forum

Archive => HOW-TO's => Archived 5.1 boards => Old HOW-TO's => Topic started by: Dessamator on 06 April, 2005, 19:53:46

Title: How To check if a file exists?
Post by: Dessamator on 06 April, 2005, 19:53:46
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?
Title:
Post by: Sedulus on 06 April, 2005, 21:11:44
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.
Title:
Post by: Dessamator on 06 April, 2005, 22:28:28
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?
Title:
Post by: Sedulus on 06 April, 2005, 23:32:26
almost...string.gsub( yourCode, '"true"', 'true' )
Title:
Post by: Dessamator on 07 April, 2005, 10:17:29
         
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)
Title:
Post by: Herodes on 07 April, 2005, 12:03:48
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 ...
Title:
Post by: Dessamator on 07 April, 2005, 12:22:40
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  !!
Title:
Post by: Herodes on 07 April, 2005, 12:39:07
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 ;)
Title:
Post by: Dessamator on 07 April, 2005, 12:56:29
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