Code Crunch

Alright. So I've been away for a while (going on a year), and I'm trying to get back into Mudlet coding so I can work at my GUI once again! Unfortunately, in bashing my brain meats around I still can't get past one issue that plagued me even back then. That issue being filtering string information in scripts. Usually because I pull it from GMCP that way.

Example (one of many places where this has been a hitch):
for k,v in pairs(gmcp.Char.Vitals.charstats) do
  if k == 2 then
    LTMRageNumber = v
  end
end

This makes LTMRageNumber (or v if you want to be simple) equal "Rage: #". The number being whatever value Rage currently is. Problem is, in this example, I'd want to filter out the "Rage: " part. How the heck do I do that? Much love to anyone who can help with this.

Comments

  • tonumber(string.match(v, "(%d+)"))

    Though you should use string.starts(v, "Rage") rather than k == 2.
  • edited December 2015
    The way I would do it is --LTMRageNumber = string.sub(v, -1)-- which starts at the first character from the end of the "v" string. Or something like that, anyhow

    Edit: Or antonius can ninja me like a coding master. w/e
  • edited December 2015
    Why use a loop to get a specific key? Just call the key.

    for k,v in pairs(gmcp.Char.Vitals.charstats) do
      if k == 2 then
        LTMRageNumber = v
      end
    end

    is the same as

    LTMRageNumber = gmcp.Char.Vitals.charstats[2]

    Unless you use Antonius' idea to get the specific rage one if it ever changes, then a loop is useful.
    Current scripts: GoldTracker 1.2, mData 1.1
    Site: https://github.com/trevize-achaea/scripts/releases
    Thread: http://forums.achaea.com/discussion/4064/trevizes-scripts
    Latest update: 9/26/2015 better character name handling in GoldTracker, separation of script and settings, addition of gold report and gold distribute aliases.
  • @Trevize The GMCP data I'm trying to track does change. Exactly why I'm using a loop.

    @Medi Thanks all the same!! Much appreciated.

    @Antonius First off, thanks a ton. Seriously! That works flawlessly, and Regex drives me up a wall every time. Also for the fact that I completely missed that string.starts() would be useful there (I blame tunnel vision). Unfortunately I don't get why the regex pattern (%d+) works. I spent a good bit of time digging through online docs and didn't see anything about the % being used in anything but a literal manner, and the online tester that I use (regexpal) completely ignores it, requiring (\d+) instead....which doesn't work at all in Mudlet. Would you mind enlightening me on that, please?
  • edited December 2015
    retired
  • Seifur said:
    That explains a good bit. I'd kiss you, but....yeah. Thanks much! :)
  • Tessa said:
    @Trevize The GMCP data I'm trying to track does change. Exactly why I'm using a loop.

    @Medi Thanks all the same!! Much appreciated.

    @Antonius First off, thanks a ton. Seriously! That works flawlessly, and Regex drives me up a wall every time. Also for the fact that I completely missed that string.starts() would be useful there (I blame tunnel vision). Unfortunately I don't get why the regex pattern (%d+) works. I spent a good bit of time digging through online docs and didn't see anything about the % being used in anything but a literal manner, and the online tester that I use (regexpal) completely ignores it, requiring (\d+) instead....which doesn't work at all in Mudlet. Would you mind enlightening me on that, please?
    I mean the ==2 part - that hasn't changed for a very long time, but it could. I track rage myself.

    Lua uses patterns, but they're -not- regex (POSIX) for reasons of speed. Lua uses a very simplified version that is similar but smaller.

    These are the character classes it allows:

    . all characters
    %a letters
    %c control characters
    %d digits
    %l lower case letters
    %p punctuation characters
    %s space characters
    %u upper case letters
    %w alphanumeric characters
    %x hexadecimal digits

    As with regex, capitalize to reverse.

    These are the magic characters:

    ( ) . % + - * ? [ ^ $

    As you note by the character classes, treat % like you would \ in normal regex. Begin classes or escape stuff. [] acts like you'd expect - custom charsets. Use ^ starting a charset to reverse it (like capitalizing the simplified charsets above). You can use - to create ranges in charsets as well. () captures just like regex, ^ and $ capture start and end of a line.

    Quantifiers are simpler and similar, but different as well. Plus and asterisk are one or more and zero or more, just like regex. A question mark acts like regex, zero or one. A dash acts like a non-greedy asterisk. One or more, but matches the least instead of the most. Equivalent to *? in regex.

    %b matches balanced strings, NOT word boundaries. That's a bit of an odd subject if you're only used to regex for triggers and such, but it comes in very handy sometimes. Not something you'd likely use often in MUD scripting though.

    Shout if you have any questions!
    Current scripts: GoldTracker 1.2, mData 1.1
    Site: https://github.com/trevize-achaea/scripts/releases
    Thread: http://forums.achaea.com/discussion/4064/trevizes-scripts
    Latest update: 9/26/2015 better character name handling in GoldTracker, separation of script and settings, addition of gold report and gold distribute aliases.
  • And in case it wasn't clear, mudlet triggers/aliases/etc use regex.

    Lua, which is used for scripting, does not and instead uses the pattern matching described above.
    Current scripts: GoldTracker 1.2, mData 1.1
    Site: https://github.com/trevize-achaea/scripts/releases
    Thread: http://forums.achaea.com/discussion/4064/trevizes-scripts
    Latest update: 9/26/2015 better character name handling in GoldTracker, separation of script and settings, addition of gold report and gold distribute aliases.
  • edited December 2015
    Trevize said:
    And in case it wasn't clear, mudlet triggers/aliases/etc use regex.

    Lua, which is used for scripting, does not and instead uses the pattern matching described above.
    Yeah, this is what had me so confused. I'm not 'great' with coding. I've always prodded at what exists, seen how it worked, altered it, researched, and applied what I knew as best as possible (I.E. no official schooling or classes). So mixing like that threw me horribly. Especially since I never really had a great need for Regex before Achaea.

    Also, yeah, I'd prefer not to gamble on the fact that the == 2 part will hold forever. Especially since I do plan to share this UI at some point (is an old thread around here about it). 8+ months away didn't change that notion. Just stalled it horribly!

    Can't say it enough that I appreciate the help from everyone. With the changes between the time I was here before and now I have a lot to edit. Not to mention getting back to squashing bugs and adding in features I wanted to. So I'm sure I'll have to give a yell again.
  • Tessa said:
    I'm not 'great' with coding. I've always prodded at what exists, seen how it worked, altered it, researched, and applied what I knew as best as possible (I.E. no official schooling or classes).
    I'm actually in the same boat. Learned regex/lua on the fly from tearing stuff down and building stuff up. Don't ever let that slow you dow! The classes I later took that included regex/etc taught less than learned just playing around.

    In short order you'll be teaching everyone else. :)
    Current scripts: GoldTracker 1.2, mData 1.1
    Site: https://github.com/trevize-achaea/scripts/releases
    Thread: http://forums.achaea.com/discussion/4064/trevizes-scripts
    Latest update: 9/26/2015 better character name handling in GoldTracker, separation of script and settings, addition of gold report and gold distribute aliases.
  • Trevize said:
    And in case it wasn't clear, mudlet triggers/aliases/etc use regex.

    Lua, which is used for scripting, does not and instead uses the pattern matching described above.
    Just for completeness sake: Mudlet also has PCRE lua bindings in case lua patterns are not powerful enough. Documentation about the library is found at http://rrthomas.github.io/lrexlib/manual.html (Look for the PCRE stuff)
Sign In or Register to comment.