Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why isn't gsub working properly?

Asked by
GShocked 150
8 years ago

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
Maybe there is something wrong with the script. Just a educated guess. Vezious 310 — 8y

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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

0
Thanks! I added 2 after the replacement parameter and it worked like a charm! So did the % sign too :) GShocked 150 — 8y
Ad

Answer this question