This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
johnnic's profile picture

String.gsub Help

Started by johnnic, 03 October 2013 - 08:33 PM
johnnic #1
Posted 03 October 2013 - 10:33 PM
Hello there. I am trying to find same words in strings that are commonly mistyped/misspelled, and then replace them with the autocorrect term. The autocorrect list is in the form "bad:good", and i know it is correctly reading them since I had it iterate through them.
Word List: http://pastebin.com/fP2kpJNp
Actual Code: http://pastebin.com/wym3xaPH
There is no error, but string.gsub is not replacing the words. Any help?
AgentE382 #2
Posted 03 October 2013 - 11:23 PM
Change this:

while stringthing:gsub(m,replcdby[p]) do
  print("FOUND")
end

Into this:

stringthing = stringthing:gsub(m,replcdby[p])

string.gsub returns the changed copy of the string. It doesn't modify the string in-place. If you want that, simply assign the return value of string.gsub back to the same string. Also, delete the while loop. string.gsub replaces all of the occurrences of the pattern with one function call.