Yuari
Documentation
Docs/Core functions/table.find

table.find

Returns the index of the first array entry equal to a value.

Syntax

table.find(t: table, value: any, init?: number) -> number?

Parameters

ParameterTypeDescription
ttableArray to search.
valueanyValue to look for.
initnumberOptional 1-based start index; negative values count back from the end.

Returns

TypeDescription
number?Index of the first match, or nil when absent.

Description

Comparison is raw equality, so two tables with the same contents are not equal and __eq is not called.

Searching ends at #t, so holes in the array stop the scan.

Example

local names = { "aoife", "mika", "nord" }
print(table.find(names, "mika"))    -- 2
print(table.find(names, "solar"))  -- nil
print(table.find(names, "aoife", 2))  -- nil

Notes

  • table.find(t, nil) — Always returns nil.
  • table.find(t, v, -2) — Starts two entries from the end.