Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find if table have table in it with given name

How to find if table have other table in it with specified name?
Or how to conver table "var1" in table "t" into string? And use "for" loop with "if" to find it.

input = "var1"

local t = {
    var1 = {[..content..]},
    var2 = {[..content..]},
    var3 = {[..content..]},
};

Now how to find, if table "t" have table "var1"?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Extending on Mikes comment: indexing a table with a non existing key returns nil. See https://www.lua.org/pil/2.5.html

And since nil evaluates to false checking for existing keys is as simple as

if t["var1"] then

Or when the identifier is valid:

if t.var1 then

If you want to make sure var1 is a table only:

if type(t.var1) == "table" then

See https://www.lua.org/pil/2.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading