I want to do a pattern match on the table below. If there is a match, take the values of the 2nd and 3rd column as answer. The first column can have 1 or multiple patterns, row 5 has only 1 pattern to match against.
local pattern_matrix = {
{{ "^small%-", "%-small%-", }, "small", 50},
{{ "^medium%-", "%-medium%-", }, "medium", 200},
{{ "^big%-", "%-big%-", }, "big", 3},
{{ "^large%-", "%-large%-", "^L%-", }, "large", 42},
{{ "%-special%-", }, "special", 5},
}
i am using the following code to find the row that matches the input:
local function determine_row(name)
for i = 1,#pattern_matrix,1 do
for _,pattern in pairs(pattern_matrix[i][1]) do --match against column 1
if name:match(pattern) then
return i --match found in row i
end
end
end
return 0
end
the result should be
determine_row("itsamedium") = 2
determine_row("blaspecialdiscount") = 5
determine_row("nodatatomatch") = 0
%-in %-small%- or %-medium%-, etc. If you're just matching the word 'small' in a larger word, the patternsmallwill work. - Allister