Make table.find return

I was wondering if it’s possible to make table.find return the table value so I can use it like this.

local Words = {
	"Yeah",
	"Hello",
	"Filler"
}
local Message = "Hello"
if Message:match(table.find(Words, Message)) then
	print(true)
end

I am doing this to see how far I can go with string manipulation.

1 Like

You mean like

function table.find(t, value)
    for _, v in pairs(t) do
        if v == value then
            return v
        end
    end
    return nil
end

local Words = {
    "Yeah",
    "Hello",
    "Filler"
}
local Message = "Hello"
if Message:match(table.find(Words, Message)) then
    print(true)
end

?

2 Likes

Yeah like that! But is there a way to not iterate through the table?

1 Like

The only way I can think of doing this is using concat.

function table.find(t, value)
    local concat_str = table.concat(t, '\0')
    local start, _ = concat_str:find(value, 1, true)
    if start then
        local index = select(2, concat_str:sub(1, start):gsub('\0', ''))
        return t[index]
    else
        return nil
    end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.