ipairs yields each array-style entry with its numeric index.

Ipairs Index

ipairs_index.lua
local middle = 
local chosenIndex = 0
local chosenColor = ""

for index, color in ipairs({"red", middle, "purple"}) do
  if color == middle then
    chosenIndex = index
    chosenColor = color
  end
end

print("middle=" .. middle)
print("chosenIndex=" .. chosenIndex)
print("chosenColor=" .. chosenColor)
local middle = 
local chosenIndex = 0
local chosenColor = ""

for index, color in ipairs({"red", middle, "purple"}) do
  if color == middle then
    chosenIndex = index
    chosenColor = color
  end
end

print("middle=" .. middle)
print("chosenIndex=" .. chosenIndex)
print("chosenColor=" .. chosenColor)
local middle = 
local chosenIndex = 0
local chosenColor = ""

for index, color in ipairs({"red", middle, "purple"}) do
  if color == middle then
    chosenIndex = index
    chosenColor = color
  end
end

print("middle=" .. middle)
print("chosenIndex=" .. chosenIndex)
print("chosenColor=" .. chosenColor)
index and value Use `ipairs` when sequence order matters and each loop step needs both the index and the value.