table/HasValue

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
([FIX] Added GitHub Link)
m (Corrected improper return of O(1) table.HasValue example)
Line 32: Line 32:
 
|Code=local mytable = { ["123"] = true, test = true }
 
|Code=local mytable = { ["123"] = true, test = true }
 
print(mytable["apple"], mytable["test"])
 
print(mytable["apple"], mytable["test"])
|Output=false true
+
|Output=nil true
 
}}
 
}}

Revision as of 22:29, 9 May 2016

 table.HasValue( )

Contents

Description

Checks if a table has a value. Please note that 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.

Arguments

table tbl

Table to check

Arguments

any value

Value to search for

Returns

boolean

Returns true if the table has that value, false otherwise

Examples

Example

Creates a table with values "123" and "test" and checks to see it the table holds value "apple"

local mytable = {"123", "test"}
print(table.HasValue(mytable, "apple"), table.HasValue(mytable, "test"))

Output:

false true

Examples

Example

Example usage of O(1) associative array structure

local mytable = { ["123"] = true, test = true }
print(mytable["apple"], mytable["test"])

Output:

nil true
Personal tools
Navigation