Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to boolean in lua

Tags:

lua

I have data received from another script where the variable has the value "false" or "true". I want to convert this value to true or false in lua datatype. Currently, I can do this long way:

if value == "false" then
  value=false
elseif value == "true" then
  value=true
end

Is there a simplest way to convert this like converting string to integer tonumber("1")

like image 397
ToiletGuy Avatar asked Dec 13 '22 06:12

ToiletGuy


1 Answers

You can also do this:

stringtoboolean={ ["true"]=true, ["false"]=false }
print(stringtoboolean[s])
like image 95
lhf Avatar answered Jan 29 '23 00:01

lhf