Functional Table Processing
Find First
Find the first value that matches a target.
Find First
find_first.lua
local needle =
local found = "none"
for _, color in ipairs({"red", "green", "blue"}) do
if found == "none" and color == needle then
found = color
end
end
print("needle=" .. needle)
print("found=" .. found)
local needle =
local found = "none"
for _, color in ipairs({"red", "green", "blue"}) do
if found == "none" and color == needle then
found = color
end
end
print("needle=" .. needle)
print("found=" .. found)
local needle =
local found = "none"
for _, color in ipairs({"red", "green", "blue"}) do
if found == "none" and color == needle then
found = color
end
end
print("needle=" .. needle)
print("found=" .. found)
find value
Finding keeps a result variable and only changes it when a matching element is seen.