toboolean function

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Schwender.exe
Prole
Posts: 25
Joined: Mon Oct 16, 2017 6:07 pm
Location: the moon
Contact:

toboolean function

Post by Schwender.exe »

edit #4
- made it easier to add more 'true' strings
- made it actually work this time :crazy:
(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
Last edited by Schwender.exe on Thu Oct 19, 2017 9:53 pm, edited 7 times in total.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: a small toboolean function

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: a small toboolean function

Post 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")?
User avatar
Schwender.exe
Prole
Posts: 25
Joined: Mon Oct 16, 2017 6:07 pm
Location: the moon
Contact:

Re: a small toboolean function

Post 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
User avatar
Schwender.exe
Prole
Posts: 25
Joined: Mon Oct 16, 2017 6:07 pm
Location: the moon
Contact:

Re: a small toboolean function

Post 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!
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: a small (and robust) toboolean function

Post 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
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: a small (and robust) toboolean function

Post by Azhukar »

Code: Select all

boolean = not not myvariable
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: a small (and robust) toboolean function

Post by grump »

Azhukar wrote: Wed Oct 18, 2017 12:08 am

Code: Select all

boolean = not not myvariable
That returns true for all strings. Not what OP wants.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: a small (and robust) toboolean function

Post 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.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: a small (and robust) toboolean function

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests