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

local charmap = {
    ["k"] = 'x',
    ["kh"] = 'x',
    ["c"] = 'k',
    ["c?"] = 'k',
    ["c!"] = 'k',
    ["ch"] = 'k',
    ["cq"] = 'k',
    ["t"] = 'c',
    ["tr"] = 'c',
    ["c"] = 'c',
    ["ch"] = 'c',
    ["d"] = 'z',
    ["g"] = 'z',
    ["gi"] = 'z',
    ["r"] = 'z',
    ["g"] = 'z',
    ["gi"] = 'z',
    ["gì"] = 'z',
    ["gí"] = 'z',
    ["g?"] = 'z',
    ["g?"] = 'z',
    ["g?"] = 'z',
    ["?"] = 'd',
    ["p"]= 'f',
    ["ph"]= 'f',
    ["n"] = 'q',
    ["ng"] = 'q',
    ["nh"] = 'q',
    ["n?"] = 'q',
    ["t"] = 'w',
    ["th"] = 'w'
}

print(string.gsub("tieng", ".", charmap))

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