71 posts
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.
1511 posts
Location
Pennsylvania
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
2088 posts
Location
South Africa
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
1511 posts
Location
Pennsylvania
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)
997 posts
Location
Wellington, New Zealand
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.
1688 posts
Location
'MURICA
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.
1511 posts
Location
Pennsylvania
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 ;)/>
997 posts
Location
Wellington, New Zealand
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.
1511 posts
Location
Pennsylvania
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!