[Lua] gsub issue I'm having

JonathinJonathin Retired in a hole.
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.
I am retired and log into the forums maybe once every 2 months. It was a good 20 years, live your best lives, friends.

Comments

  • Are you getting the string from a trigger capture or via gmcp?
    retired
  • JonathinJonathin Retired in a hole.
    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.
    I am retired and log into the forums maybe once every 2 months. It was a good 20 years, live your best lives, friends.
  • JonathinJonathin Retired in a hole.
    Just as an addendum: I'm trying to change all of my clan channels to fit this description.
    I am retired and log into the forums maybe once every 2 months. It was a good 20 years, live your best lives, friends.

  • 
    ^\((.+)\)\: (\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 Retired in a hole.
    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.
    I am retired and log into the forums maybe once every 2 months. It was a good 20 years, live your best lives, friends.

  • 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 Retired in a hole.
    It's not about being efficient, it's about being user friendly for anyone that wants to use it.
    I am retired and log into the forums maybe once every 2 months. It was a good 20 years, live your best lives, friends.
  • 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.
  • JonathinJonathin Retired in a hole.
    That works perfectly. I was trying to over complicate waay too much.
    I am retired and log into the forums maybe once every 2 months. It was a good 20 years, live your best lives, friends.
  • 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 Retired in a hole.
    oooo i like
    I am retired and log into the forums maybe once every 2 months. It was a good 20 years, live your best lives, friends.
Sign In or Register to comment.