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
| Parameter | Type | Description |
|---|---|---|
t | table | Array to search. |
value | any | Value to look for. |
init | number | Optional 1-based start index; negative values count back from the end. |
Returns
| Type | Description |
|---|---|
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 returnsnil.table.find(t, v, -2)— Starts two entries from the end.