Question about parsing and echoing a string

Posted by Ircria on Sun 28 Nov 2010 02:52 AM — 4 posts, 16,860 views.

#0
Hello.

I've come across a slight problem in one of my functions. I am trying to take a string such as:
"@health/@maxhealthh, @manam"(without quotes)
and Tell/Note a coloured(depending on the amount):
"3317/3317h, 2341m"(without quotes)

My main problem is parsing out the @health, @maxhealth, and @mana, add a colour, replace it with the proper amount(which is stored in a lua variable, not a MUSHclient variable), and Tell/Note it to the output.

To my knowledge, I couldn't use a string.gsub to replace it because of the colour formatting. I was thinking of doing it character by character, but this would be firing anywhere from once every few seconds to several times per second, so a noticeable slowdown would be a problem.

If somebody could point me in the right direction on this, or tell me if I'm missing something, that would be great.

Thank you.
Australia Forum Administrator #1
I did a function a while ago that does the find/replace of @xxx variables in a string:


str = "@health/@maxhealth, @mana"

    local errors =  {} -- no errors yet
  
    -- function to do the replacements for string.gsub
    
    local function replace_variables (s)
      s = string.sub (s, 2)  -- remove the @
      replacement = GetPluginVariable ("", s)    -- look up variable in global variables
      if not replacement then  -- not there, add to errors list
        table.insert (errors, s)
        return
      end -- not there
      return replacement  -- return variable
    end -- replace_variables 
    
    -- replace all variables starting with @
    
    local fixed_str = string.gsub (str, "@%a[%w_]*", replace_variables)
    
    -- report any errors
    
    if #errors > 0 then
      for k, v in ipairs (errors) do
        ColourNote ("white", "red", "Variable '" .. v .. "' does not exist")
      end -- for
      return
    end -- error in replacing

print (fixed_str)


That would be quick enough. As for the colours, you haven't said how you would specify those, but another string.gsub could be used. Do the colours change, or could they be in the original string?
Amended on Sun 28 Nov 2010 03:52 AM by Nick Gammon
#2
The colours change depending on the value(or percentage of the value when compared to the maximum value), but sometimes retain the previous colour. For example, health above or equal to 75% would be green, above or equal to 25% and below 75% would be yellow, below 25% and above 0% would be red, and 0% would be grey. But all literally printed characters(a slash or comma after), or maxhealth would take on the colour set by health.

Your post helped a lot, and I think I might be able to adapt that to my purpose. If not, I'll post again in this thread with more details about what's going wrong. Thank you again.
Australia Forum Administrator #3
Doing a string.gsub is surprisingly fast. For example, doing 100,000 of them took me 0.655 seconds:


 local fixed_str

    start = utils.timer ()
    for i = 1, 100000 do
      fixed_str = string.gsub (str, "@%a[%w_]*", replace_variables)
    end -- for
    print (string.format ("Time taken = %0.6f", utils.timer () - start))   


Output:


Time taken = 0.655193
55/100, 2000


You might need to replace GetPluginVariable by GetVariable depending on your situation.