Table.find

From GiderosMobile
Revision as of 22:04, 20 January 2023 by MoKaLux (talk | contribs) (Created page with "__NOTOC__ '''Available since:''' Gideros 2022.3<br/> '''Class:''' table<br/> === Description === Within the given array-like table ''t'', find the first occurrence of ''v...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Available since: Gideros 2022.3
Class: table

Description

Within the given array-like table t, find the first occurrence of value, starting from index or the beginning if not provided. If the value is not found, nil is returned.

(any) = table.find(t,value,index)
A linear search algorithm is performed

Parameters

t: (table) source table
value: (any) value to find
index: (number) index to start the search at optional

Return values

Returns (any) the index of first occurence of value or nil if not found

Example

local t = {"a", "b", "c", "d", "e"}
print(table.find(t, "d")) --> 4
print(table.find(t, "z")) --> nil, because z is not in the table
print(table.find(t, "b", 3)) --> nil, because b appears before index 3