Welcome to the Achaea Forums! Please be sure to read the Forum Rules.

[Lua] gsub issue I'm having

JonathinJonathin Grand Rapids, MIMember Posts: 3,324 @@ - Legendary Achaean
I'm trying to get the channel string to sub from

 (Clan Channel Long Name): you say, "Hi."

to

 (clt7): You say, "Hi."

but I'm having issues with the pattern matching bit in the gsub. I can get it to change all of the words or none of them. There's obviously something I'm overlooking, but I'm at a loss of how to actually get it to work the way I want.

My site will remain up, but will not be maintained. The repository will continue to have scripts added to it if I decide to play another game. Maybe I'll see you around in Starmourn!
Tutorials and scripts  The Repository

Tagged:

Comments

  • SeifurSeifur Member Posts: 64 ✭✭✭ - Distinguished
    Are you getting the string from a trigger capture or via gmcp?
    retired
  • JonathinJonathin Grand Rapids, MIMember Posts: 3,324 @@ - Legendary Achaean
    edited December 2014
    Gmcp, sorry. The main problem I'm having is that I can't figure out how to set a boundary so that mudlet knows when to stop subbing the text.

    My site will remain up, but will not be maintained. The repository will continue to have scripts added to it if I decide to play another game. Maybe I'll see you around in Starmourn!
    Tutorials and scripts  The Repository

  • JonathinJonathin Grand Rapids, MIMember Posts: 3,324 @@ - Legendary Achaean
    Just as an addendum: I'm trying to change all of my clan channels to fit this description.

    My site will remain up, but will not be maintained. The repository will continue to have scripts added to it if I decide to play another game. Maybe I'll see you around in Starmourn!
    Tutorials and scripts  The Repository

  • ErnamErnam Member Posts: 2,416 ✭✭✭✭✭ - Grand Achaean

    
    ^\((.+)\)\: (\w+) (say|says), "(.+)"$
    

    I would recommend using deleteLine() and cecho() instead of gsub, or if you're not using color, just use replace().

    For example:

    
    local clans = 
    {
        Dawnguard = "clt1",
        Project 9 = "clt2",
        -- etc
    }
    
    local colors =
    {
        Dawnguard = "white",
        Project 9 = "pink",
    }
    
    if table.index_of(clans,matches[2]) then
        deleteLine()
        cecho("\n<"..colors[matches[2]..">("..clans[matches[2]].."): "..matches[3].." "..matches[4]..", \""..matches[5].."\"" 
    end
    

    (haven't actually tested that, probably a typo or two in there, but it'll do what you're wanting to do)

    A (slightly) more efficient way to do this would be to define the variables in a script instead of in the trigger itself.
  • JonathinJonathin Grand Rapids, MIMember Posts: 3,324 @@ - Legendary Achaean
    edited December 2014
    The data is already present in the gmcp table, which is what I'm trying to use. I'd really like to keep my script as light as possible without having to add or remove things when I join or quit a clan. If there is no way to do it, I suppose I'll have no choice but to use a table, but I'd really rather not.

      text = "[0;37;40m(Jester's Carousal): You say, \"Hi.\"[0;37;40m",
      talker = "Mosr",
      channel = "clt7"

    This is all being done with gmcp in a script, no triggers at all.

    My site will remain up, but will not be maintained. The repository will continue to have scripts added to it if I decide to play another game. Maybe I'll see you around in Starmourn!
    Tutorials and scripts  The Repository

  • ErnamErnam Member Posts: 2,416 ✭✭✭✭✭ - Grand Achaean

    I mean, sure, that's slightly more efficient, but at some point, people should try to remember that a "normal" PC these days performs billions of operations per second, and it's mathematically asinine to fine-tune efficiency on text pattern matching and string operations.

    If you really cared about efficiency/overhead, you would never be using lua and/or mudlet in the first place.  It's grossly inefficient, compared to what it could do, theoretically, but you'd never know it, because it's still using < 2-3% of your CPU's capability (almost all of which is due to graphics, database handling, saving logs, and network utilization).
  • JonathinJonathin Grand Rapids, MIMember Posts: 3,324 @@ - Legendary Achaean
    It's not about being efficient, it's about being user friendly for anyone that wants to use it.

    My site will remain up, but will not be maintained. The repository will continue to have scripts added to it if I decide to play another game. Maybe I'll see you around in Starmourn!
    Tutorials and scripts  The Repository

  • AntoniusAntonius Member Posts: 4,966 @@ - Legendary Achaean
    Shouldn't something like text:gsub("%(.+%)", "(" .. channel .. ")") work? Basically matches a ( followed by some characters followed by a ) to do the replacement. Hard to say what you're doing wrong without seeing what you've actually tried.
    SeifurJonathin
  • JonathinJonathin Grand Rapids, MIMember Posts: 3,324 @@ - Legendary Achaean
    That works perfectly. I was trying to over complicate waay too much.

    My site will remain up, but will not be maintained. The repository will continue to have scripts added to it if I decide to play another game. Maybe I'll see you around in Starmourn!
    Tutorials and scripts  The Repository

  • KeneanungKeneanung Member Posts: 616 ✭✭✭✭ - Eminent
    You could even use a little more lua pattern magic: %b matches a balanced pair of the 2 following characters. So you could define your pattern as "^%b()"
  • JonathinJonathin Grand Rapids, MIMember Posts: 3,324 @@ - Legendary Achaean
    oooo i like

    My site will remain up, but will not be maintained. The repository will continue to have scripts added to it if I decide to play another game. Maybe I'll see you around in Starmourn!
    Tutorials and scripts  The Repository

Sign In to Comment.