Page 1 of 3
toboolean function
Posted: Tue Oct 17, 2017 10:07 pm
by Schwender.exe
edit #4
- made it easier to add more 'true' strings
- made it actually work this time
(thanks to grumpy and zorg for help fixing it, and hi reddit!)
Code: Select all
local tobool_True = {"1","true","t","yes","y"}
function tobool(str)
if type(str) ~= "string" then str = tostring(str) end
str = string.lower(str)
for i=1,#tobool_True do
if str == string.lower(tobool_True[i]) then
return true
end
end
return false
end
Re: a small toboolean function
Posted: Tue Oct 17, 2017 10:14 pm
by zorg
On the other hand, lua treats everything except false and nil as logically true, so a simple
Code: Select all
if x then
-- true
else
-- false
end
Re: a small toboolean function
Posted: Tue Oct 17, 2017 10:18 pm
by grump
Extremely robust... toboolean(true) returns nil though, which means true is false.
And are you sure that toboolean(1) should equal true, although toboolean("1") equals false? What about toboolean("broken code")?
Re: a small toboolean function
Posted: Tue Oct 17, 2017 11:43 pm
by Schwender.exe
grump wrote: ↑Tue Oct 17, 2017 10:18 pm
Extremely robust... toboolean(true) returns nil though, which means true is false.
And are you sure that toboolean(1) should equal true, although toboolean("1") equals false? What about toboolean("broken code")?
going to re-write it, mb
Re: a small toboolean function
Posted: Tue Oct 17, 2017 11:48 pm
by Schwender.exe
zorg wrote: ↑Tue Oct 17, 2017 10:14 pm
On the other hand, lua treats everything except false and nil as logically true, so a simple
Code: Select all
if x then
-- true
else
-- false
end
yea, I re-wrote it, sorry!
Re: a small (and robust) toboolean function
Posted: Tue Oct 17, 2017 11:56 pm
by grump
Schwender.exe wrote: ↑Tue Oct 17, 2017 10:07 pm
Code: Select all
function toboolean(str)
str = tostring(str)
if str == "1" or "true" or "t" then
return true
else
return false
end
end
Now it's even more broken. This code always returns true.
You probably want this:
Code: Select all
function tobool(val)
if type(val) == "string" then
return val == "true" or val == "t" or val == "1" -- return true for these values, false for all other strings
end
return val -- not a string, use default behavior
end
Re: a small (and robust) toboolean function
Posted: Wed Oct 18, 2017 12:08 am
by Azhukar
Re: a small (and robust) toboolean function
Posted: Wed Oct 18, 2017 12:13 am
by grump
Azhukar wrote: ↑Wed Oct 18, 2017 12:08 am
That returns true for all strings. Not what OP wants.
Re: a small (and robust) toboolean function
Posted: Wed Oct 18, 2017 12:25 am
by Azhukar
grump wrote: ↑Wed Oct 18, 2017 12:13 amThat returns true for all strings. Not what OP wants.
The solution to that is to change what the OP wants since OP is wrong. If you're going to redefine what boolean means in the language you're using, you'll end up spending a lot more effort on nothing.
Re: a small (and robust) toboolean function
Posted: Wed Oct 18, 2017 12:46 am
by grump
Azhukar wrote: ↑Wed Oct 18, 2017 12:25 amThe solution to that is to change what the OP wants since OP is wrong. If you're going to redefine what boolean means in the language you're using, you'll end up spending a lot more effort on nothing.
You need to enhance your calm. OP just wants to parse a string and convert some values to boolean. There is nothing inherently wrong with that. Maybe he wants to write a config file parser, who knows.