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"))

License

BSD-3-Clause

Keywords

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

Latest Posts

Remotely backing up PFsense Configuration (BASH)
FFMPEG: Converting RMVB to X264 MP4 (BASH)
Recursively print table (print_r equivalent) (LUA)
Bulk Delete Comments from (Self-Hosted) JIRA Issues (Misc)
Add a static entry to the ARP table (BASH)
SSL Cipher Hex codes to Human Readable Names (Misc)
Convert Ascii to Binary (BASH)
Intercepting Outbound DNS Queries (BASH)
Handle Google Verification files within NGinx Configuration (NGinx)
Getting WhatsApp Rich Snippet Previews Working (Misc)

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