Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why isn't gsub working properly?

I have a JSONEncoded table that looks like this:

ImgScript = '["27.83","301.2","27.3","301.15","302.1","301.4","27.26","301.1","27.19","301.1","27.18","301.9","27.3", etc...]'

Now, don't let those numbers fool you! 27.83 actually means 27 to the power of 83, or 27^83. I can't use a ^ symbol or else JSONDecode won't work. This string is really long, so I want to shorten it. To shorten it, I did the following:

ImgScript = string.gsub(ImgScript, '"', '')
ImgScript = string.gsub(ImgScript, '.1,', ',')

So the first line basically removes all of the "quotation" marks. The second line is supposed to replace all the numbers that are to the power of 1, because that is redundant, with the number itself. So 301.1 would become 301.

This worked at first, but some sort of change I made broke the script. Instead of replacing all of the .1, with ,, some of them are replaced with ., which then gives me an error "Can't Parse JSON" when I try to JSONDecode.

So why does my second line of code replace .1, with ., instead of ,?

1 comment

1

Maybe there is something wrong with the script. Just a educated guess.

1 answer

✓ Accepted answer

Most of the string processing functions work with string patterns. Patterns describe a more general type of string. For example, str:find( "%d" ) will tell you where the first digit is (not the first percent-sign-followed-by-the-letter-d).

"." is the pattern that means "any single character".

Your search pattern is then "any character, followed by a 1 then a comma". Thus "21," --> ","

To match a literal . you can either escape it by preceding it with a %: "%." or you can pass another parameter (I think it's two after the replacement) to :gsub to disable pattern matching

1 comment

0

Thanks! I added 2 after the replacement parameter and it worked like a charm! So did the % sign too :)

This discussion is preserved as it was. New answers, comments, and votes are closed.