Page 3 of 3

Re: toboolean function

Posted: Fri Oct 20, 2017 1:55 am
by zorg
I sense that this may have been one of those terminological issues, i.e. "what 'toboolean' means to /me/ isn't what it means to /you/"; also, the initial explanation was probably missing a lot. Such is life, but at least now we know what was meant; an "arbitrary string to boolean mapping function".
I'd still highlight that this is atypical lua behaviour though, so strictly speaking it's not how booleans work in lua; but other than that, it may be a valid thing for someone to want to do. (As Azhukar already said)

Re: a small toboolean function

Posted: Fri Oct 20, 2017 2:13 am
by Schwender.exe
brogrammer wrote: Fri Oct 20, 2017 1:47 am
Schwender.exe wrote: Fri Oct 20, 2017 1:29 am yea, sorry for the bad and broken code, atleast it works now and actually does what it's supposed to
no its slow and unnecessarily complicated and does redefine what true means
you know that anything ~= nil and false is supposed to be true right
its still bad code
why not just take the function grump has posted and just add the words you want to be recognized as true
I re-wrote it again, not sure if you saw the new version of it

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: toboolean function

Posted: Fri Oct 20, 2017 2:24 am
by grump

Code: Select all

print(0x1337c0de and true, tobool(0x1337c0de) and true)
> true    false