lua-users home
lua-l archive

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


>function change (src, find, rep)
>    src = gsub (src, "(%w+)", function (word)
>         if word == %find then
>              return %rep
>         else
>              return word
>         end
>    end)
>    return src
>end
>

Or:

function change(src, tab)
   src = gsub(src, "(%w+)", function(word) return tab[word] or word end)
   return src
end

src = change("this is the answer", {this = "these", is = "are", answer
= "answers"})

or even

function changer(tab)
   return function(src)
       src = gsub(src, "(%w+)", function(word) return %tab[word] or word
end)
   end
end

pluralise = changer {this = "these", is = "are", answer = "answers"}

src = pluralise(src)