Quirrel4.41.0

types.Table.hasvalue

Method on every table
pure table.hasvalue(value): any

Reports whether value is stored under any key in the table.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

valueanythe value to search for

Return value

true if some key maps to a value equal to value, false otherwise.

Notes

Takes exactly one argument; t.hasvalue() throws a wrong-number-of-parameters error.

Walks every slot in the table's current iteration order and stops at the first match, so the answer does not depend on that order - only whether a match exists does. Equality is raw (like <=>'s fallback, never a _cmp metamethod): numbers compare by value, and tables, arrays and instances compare by identity, so a different table with equal contents is not a match.

Use findvalue instead when the match itself, or its key, is needed rather than only a yes/no answer.

Example

examples/types/table/hasvalue.nut
let t = {a=1, b=2, c=1}
println("t.hasvalue(2) =", t.hasvalue(2))
println("t.hasvalue(99) =", t.hasvalue(99))

// value equality is raw: two distinct tables with equal contents don't match
let marker = {tag=1}
let u = {slot=marker}
println("u.hasvalue({tag=1}) =", u.hasvalue({tag=1}))   // a different table, not the same one
println("u.hasvalue(marker) =", u.hasvalue(marker))    // the very same table
Output:
t.hasvalue(2) = true
t.hasvalue(99) = false
u.hasvalue({tag=1}) = false
u.hasvalue(marker) = true

See also

hasindexReports whether key is a slot in the table.
findvalueReturns the first value for which callback returns a truthy result.
rawinReports whether key is a slot in the table, without calling a metamethod.
tableclass index