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 <ANY>
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):
-- should also print "blue" ("setting." should not be required);
print(FindValueFromPattern(tbl, "module.<ANY>.color"));
Rather than returning found values straight away, I may have to change the logic to insert found values in a table and then return the table after the for-loop but I wrote this quickly to illustrate the problem.
So the question is, what would that pattern look like? Thank you.
Aucun commentaire:
Enregistrer un commentaire