Garry's Mod Wiki

table.HasValue

  boolean table.HasValue( table tbl, any value )

Description

Checks if a table has a value.

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: Tables: Bad Habits
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.

Arguments

1 table tbl
Table to check
2 any value
Value to search for

Returns

1 boolean
Returns true if the table has that value, false otherwise

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

Example

Example usage of O(1) associative array structure

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