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

Using string.gsub multiple times?

Started by gknova61, 07 March 2013 - 03:31 PM
gknova61 #1
Posted 07 March 2013 - 04:31 PM
Say if you're in the following situation

You have something like asdfghj let's declare this as a variable

string = asdafghsja

and you need to turn the a's in there to s's and the s's to a's, you'd use string.gsub right like this?

string.gsub(string,"a","s")
string.gsub(string,"s","a")


I want to turn the a's into s and the s's into a without it conflicting and turning all the a's into s's then all the s's it just changed into all a's. Basically I want this final product:

sadsfghajs

Instead of the following which is what the above code would yield:

aadafghaja

In case it's still not clear, like a way to use gsub but to replace multiple things at the exact same time and not in order so they don't overwrite eachother.
SuicidalSTDz #2
Posted 07 March 2013 - 04:38 PM
I've never personally needed to use string.gsub like you need to use it. If this doesn't help then i'm sure someone else would be able to solve your problem.

EDIT: I am guessing you will need to get where all the a's are at and pull them from the string, then turn them into s's and insert them back into the string with the position they were in before you pulled them. Do the same with the s's, but pull them both at the same time. (If that makes sense) You can get the location of a char in the string by using string.find() which is in the link I gave you
remiX #3
Posted 07 March 2013 - 05:24 PM
Just change all the other s's before changing the a's to s's to - or something odd.
s = 'asdafghsja' -- if u name it string, it will override the string api!

string.gsub(s, 's', '~'
string.gsub(s,"a","s")
string.gsub(s,"s","a")
string.gsub(s, '~', 's')

print(s) -- should be asdafghsja
SuicidalSTDz #4
Posted 07 March 2013 - 05:29 PM
if u name it string, it will override the string api!
Shocker… ;)/> Now remiX, does that mean if I name a table "table", it will override the table api?! (Hehe)
immibis #5
Posted 07 March 2013 - 06:03 PM
if u name it string, it will override the string api!
Shocker… ;)/>/> Now remiX, does that mean if I name a table "table", it will override the table api?! (Hehe)
If you name any global "table" it will override the table API.
Kingdaro #6
Posted 07 March 2013 - 06:06 PM
You can do this.


s = string.gsub('asdafghsja','[as]',function(letter)
  return letter == 'a' and 's' or 'a'
end)

It finds every match of an a or an s, and replaces it with an a if it's an s, and vice versa.
SuicidalSTDz #7
Posted 07 March 2013 - 06:34 PM
if u name it string, it will override the string api!
Shocker… ;)/>/> Now remiX, does that mean if I name a table "table", it will override the table api?! (Hehe)
If you name any global "table" it will override the table API.
Yeah, kinda figured. Why would you even want to name a table "table or a string "string"? Pretty useless names. Although I see it sometimes in Ask A Pro… People really need to read the Lua Reference Manual. Cloudy should link it in the confirmation email you get when you join ;)/>
immibis #8
Posted 07 March 2013 - 07:42 PM
If you name any global "table" it will override the table API.
Yeah, kinda figured. Why would you even want to name a table "table or a string "string"? Pretty useless names. Although I see it sometimes in Ask A Pro… People really need to read the Lua Reference Manual. Cloudy should link it in the confirmation email you get when you join ;)/>/>
You can actually overwrite string with a string without breaking its use as an API. Strings have a hidden metatable with an __index field pointing to a table with all the string functions - that's how myString:sub etc can work.
SuicidalSTDz #9
Posted 07 March 2013 - 08:01 PM
You can actually overwrite string with a string without breaking its use as an API. Strings have a hidden metatable with an __index field pointing to a table with all the string functions - that's how myString:sub etc can work.
That is why you are a Lua God… I mean, it does make sense now that I wrap my brain around the idea. Again, I like my box! I like staying in the box assigned to me!