Check if table has element



Published: 2017-05-23 11:47:23 +0000
Categories: LUA,

Language

LUA

Description

Simple function to check whether a specific key exists within a table in LUA

Similar to

  • Python: if "foo" in dict:
  • PHP: array_key_exists("foo",$array);

Snippet

function tableHasKey(table,key)
    return table[key] ~= nil
end

Usage Example

local t={ ["foo"] = "bar" }

print(tableHasKey(t,"foo"))
print(tableHasKey(t,"notthere"))

Video Example

License

BSD-3-Clause

Keywords

Table, array, dict, has key, has element, contains,

Latest Posts


Copyright © 2019 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk and snippets.6zdgh5a5e6zpchdz.onion