Find first in Lua

Find this useful? Support us: Star on GitHub 6
Category: Array | Language: Lua

In Lua, arrays are indexed starting from 1. To find the first element of an array, you simply need to reference it with the number 1 inside square brackets. Here is an example:

-- Define an array
myArray = {"apple", "banana", "cherry"}

-- Find the first element
firstElement = myArray[1]

-- Print the result
print(firstElement) -- Outputs: apple

In this example, we first define an array with three elements. Then we find the first element by referencing it with myArray[1]. Finally, we print the result to the console using the print function. The output will be "apple", which is the value of the first element in the array.