lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



--- On Tue, 5/21/13, S. Fisher <expandafter@yahoo.com> wrote:


> 
> function substitute(s, substitutions)
>   sorted = {}
>   for k, _ in pairs( substitutions ) do
>     table.insert( sorted, k )
>   end
>   table.sort( sorted, function (a,b) return #a > #b
> end )
>   for _, key in ipairs( sorted ) do
>     s,cnt = string.gsub(s, escape(key),
> substitutions)
>   end
>   return s
> end
> 

function substitute(s, substitutions)
  local sorted = {}
  for k, _ in pairs( substitutions ) do
    table.insert( sorted, k )
  end
  table.sort( sorted, function (a,b) return #a > #b end )
  for _, key in ipairs( sorted ) do
    s = string.gsub(s, escape(key), substitutions)
  end
  return s
end