Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Question about parsing and echoing a string
|
Question about parsing and echoing a string
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Ircria
(24 posts) Bio
|
| Date
| Sun 28 Nov 2010 02:52 AM (UTC) |
| Message
| 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. | | Top |
|
| Posted by
| Nick Gammon
Australia (23,165 posts) Bio
Forum Administrator |
| Date
| Reply #1 on Sun 28 Nov 2010 03:50 AM (UTC) Amended on Sun 28 Nov 2010 03:52 AM (UTC) by Nick Gammon
|
| Message
| 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? |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Ircria
(24 posts) Bio
|
| Date
| Reply #2 on Sun 28 Nov 2010 04:00 AM (UTC) |
| Message
| 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. | | Top |
|
| Posted by
| Nick Gammon
Australia (23,165 posts) Bio
Forum Administrator |
| Date
| Reply #3 on Sun 28 Nov 2010 08:46 PM (UTC) |
| Message
| 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. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
15,146 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top