Title

Thursday, 5 February 2015

Lua: improving a multiple words replacement with local values [on hold]


I have 3 locals numbers (total, elapsedtime and percent) and a string. This string the user can use a text field to customize the order and which numbers is shown to him. The user can type {data1} to show total, {data2} to show the elapsedtime, and {data3} for percentage.

For instance, a guy enter the text: "i got the time: {data2}, {data3}% of {data1} minutes". The function returns: "i got the time: 1000, 20% of 5000 minutes"

I've created the function below to handle the replacement, but i am almost sure that exists a better way to achieve this result. So my question is how can i improve this big function?

 --user typed   local user_chose = "i got the time: {data2}, {data3}% of {data1} minutes"     --local values for replacement   local total = 5000   local elapsedtime = 1000   local percent = 20     --replace function    local _data1, _data2, _data3   local replace_func = function (data)   if (data == "data1") then   return _data1   elseif (data == "data2") then   return _data2   elseif (data == "data3") then   return _data3   end   end     function create_text (data1, data2, data3)  --update the 3 locals used on replace_func   _data1, _data2, _data3 = data1, data2, data3  --do the gsub   return string.gsub ( user_chose, "{(.-)}", replace_func)   end     --print the string to the user   print (create_text (total, elapsedtime, percent))
Answer

The user can type {data1} to show total, {data2} to show the elapsedtime, and {data3} for percentage

Those are bad macro names, just as they would be bad variables names. The user should be able to type {total} to show total, {elapsedtime} to show the elapsedtime, etc.

how can i improve this big function?

The most obvious change would be to pass a table to gsub, so you don't need to write the replacement function.

function expand(format, values)   return (format:gsub("{(.-)}", values))  end    local usertext = "i got the time: {elapsedtime}, {percent}% of {total} minutes"  local expanded = expand(usertext, {total = 50000, elapsedtime = 1000, percent = 20})

If you're not adverse to extending string (some people find this reprehensible), you can get nicer usage syntax:

function string:expand(values)   return (self:gsub("{(.-)}", values))  end    local expanded = usertext:expand { total = 50000, elapsedtime = 1000, percent = 20 }

If you insist on using positional macro names, you can do this:

function string:expand(...)   local args = {...}   local function getarg(i) return args[tonumber(i)] end   return (self:gsub('{data(%d+)}', getarg))  end  local usertext = "i got the time: {data2}, {data3}% of {data1} minutes"  local expanded = usertext:expand(50000, 1000, 20)

No comments:

Post a Comment