Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the value of a boolean (set by user) with a variable string?

The user sets a boolean to true or false.

That does (exemple)

ElementNameone = true
ElementNametwo = false
ElementNamethree = true

Etc.

Now I have a string that is loaded from a file. The string called name can have values that are Nameone, Nametwo, Namethree, etc. Anyone of them at a time. Now I would like to be able to do this

if Element .. name == true then
do something

Except I don't know how to do this properly.

I've tried to do

if not not ("Element" .. name) then

But it does not work.

Can anyone help ?

Thanks

like image 881
arj Avatar asked Oct 22 '25 05:10

arj


1 Answers

Try this:

if _G["Element" .. name] == true then
    -- do something
end

Note that this will work only if the variables set by the user (ElementNameone, .. etc.) are globals.

like image 130
Lorenzo Donati -- Codidact.com Avatar answered Oct 23 '25 21:10

Lorenzo Donati -- Codidact.com



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!