Garry's Mod Wiki

Revision Difference

table.HasValue#551731

<function name="HasValue" parent="table" type="libraryfunc"> <description> Checks if a table has a value. <warning>This function is **very inefficient for large tables** (O(n)) and should probably not be called in things that run each frame. Instead, consider a table structure such as example 2 below. Also see: <page>Tables: Bad Habits</page></warning> <note>For optimization, functions that look for a value by sorting the table should never be needed if you work on a table that you built yourself.</note> </description> <realm>Shared and Menu</realm> <file line="97-L106">lua/includes/extensions/table.lua</file> <file line="105-L110">lua/includes/extensions/table.lua</file> <args> <arg name="tbl" type="table">Table to check</arg> <arg name="value" type="any">Value to search for</arg> </args> <rets> <ret name="" type="boolean">Returns true if the table has that value, false otherwise</ret> </rets> </function> <example> <description>Creates a table with values "123" and "test" and checks to see it the table holds value "apple"</description> <code> local mytable = {"123", "test"} print(table.HasValue(mytable, "apple"), table.HasValue(mytable, "test")) </code> <output>false true</output> </example> <example> <description>Example usage of O(1) associative array structure</description> <code> local mytable = { ["123"] = true, test = true } print(mytable["apple"], mytable["test"]) </code> <output>nil true</output> </example>