table.HasValue

From Garry's Mod
Jump to: navigation, search
 table.HasValue( )

Contents

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.

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.

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