Responsive Ad Area

Share This Post

test

Lua – Using string.find to return true if between 2 special characters

I need a pattern that will work with string.find (or string.match if necessary) that will return true if a “table path” string matches. This is my function:

local function FindValueFromPattern(myTable, pattern, previousPath)
    for key, value in pairs(myTable) do
        local path;

        if (not previousPath) then
            path = key;
        else
            path = string.format("%s.%s", previousPath, key);
        end

        if (path:find(pattern)) then
            return value;
        elseif (type(value) == "table") then
            value = FindValueFromPattern(value, pattern, path);

            if (value ~= nil) then
               return value;
            end
        end
    end

    return nil;
end

local tbl = {}
tbl.settings = {};
tbl.settings.module = {};
tbl.settings.module.appearance = {};
tbl.settings.module.appearance.color = "blue";

print(FindValueFromPattern(tbl, "settings.module.appearance.color")); -- prints "blue";

The code above works BUT I want to now change the pattern to:

"module.<ANY>.color" where is any child table of “module” and also has a child table called “color”, so when traversing down the table, a value will be returned regardless of what table is used (does not have to be the appearance table).

Rather than returning found values, I may have to change the logic to return a table containing all found values but I wrote this quickly to illustrate the problem.

So the question is, what would that pattern look like? Thank you.


Lua – Using string.find to return true if between 2 special characters
Lua – Using string.find to return true if between 2 special characters
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar