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 © 2022 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk, http://phecoopwm6x7azx26ctuqcp6673bbqkrqfeoiz2wwk36sady5tqbdpqd.onion and http://snippets.bentasker.i2p
hit counter