table.indexOf()

Type Function
Library table.*
Return value Number
Revision Release 2024.3703
Keywords table, array

Overview

Returns the integer index of an element within an array, or nil if the element is not in the array. The search goes through the length of the array as determined by #t whose value is undefined if there are holes.

Gotchas

This function can not be used to locate a child table/array within the array being searched.

Syntax

table.indexOf( t, element )
t (required)

Array. The array to be searched.

element (required)

String or Number. The element to search for within array t.

Example

local t = { 1, 3, 5, 7, 9, "a", "b" }
print( table.indexOf( t, 9 ) )    --> 5
print( table.indexOf( t, 3 ) )    --> 2
print( table.indexOf( t, "b" ) )  --> 7