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

String.gsub doesn't work correctly ?

Asked by
LeadRDRK 437 Moderation Voter
7 years ago

Hi. I recently tried out gsub and figuring how it works, and I ran into a problem. I have a script like this

01local charmap = {
02    ["k"] = 'x',
03    ["kh"] = 'x',
04    ["c"] = 'k',
05    ["c?"] = 'k',
06    ["c!"] = 'k',
07    ["ch"] = 'k',
08    ["cq"] = 'k',
09    ["t"] = 'c',
10    ["tr"] = 'c',
11    ["c"] = 'c',
12    ["ch"] = 'c',
13    ["d"] = 'z',
14    ["g"] = 'z',
15    ["gi"] = 'z',
View all 35 lines...

but instead of printing "tieq", it prints "tieqw" (look at the character map for reference). Please fix it for me. Thanks in advance.

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago

When I tested, it printed what it was supposed to: wieqz.

  • The first letter, t was matched and replaced with w.

  • The third and fourth letters, n and g were matched and replaced with q.

  • The third letter, n, was matched again and "replaced" with z

This is the product of gsub matching two different overlapping patterns.

0
is there a workaround for this ? LeadRDRK 437 — 7y
Ad

Answer this question