lua indexof

The Lua programming language does not have a built-in function for finding the index of a specific value in a table or string. However, you can create a custom function to achieve this functionality. Here is an example of how you can implement an indexof function in Lua:

function indexof(table, value)
    for i, v in ipairs(table) do
        if v == value then
            return i
    return -1

-- Example usage
local fruits = {"apple", "banana", "orange", "grape"}
print(indexof(fruits, "banana")) -- Output: 2

local numbers = {10, 20, 30, 40, 50}
print(indexof(numbers, 30)) -- Output: 3

In the above code, the indexof function takes two parameters: the table to search in and the value to find. It uses a for loop to iterate over each element in the table. If the current element matches the desired value, it returns the index of that element. If no match is found, it returns -1.

To use the indexof function, you simply call it and pass in the table and value you want to search for. The function will return the index of the value if it is found, or -1 if it is not found.