Register forum user name Search FAQ

Gammon Forum

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.
 Entire forum ➜ MUSHclient ➜ General ➜ Simple arithmethics, adding values

Simple arithmethics, adding values

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Eloge   (20 posts)  Bio
Date Mon 20 Sep 2010 02:48 PM (UTC)
Message
I now got (from my trigger with ColourNote) the output - the mud sends four lines:

Clio 306
Divine 248
Clio 331
Divine 255
(Clion and Divine are btw. the names of weapons). The values comes, as remarked, from four different lines (during one combatround).

What i want to have is something like:

Clio 306, Divine 248, Clio 331, Divine 255, for a total of 1.140.

The problem is that as well ColourNote as ColourTell (and I presume, also note) send linefeeds. Can linefeeds be avoided?

How is 2) the arithmetic done. The values (of 306, 248, 331 and 205) are from the "%1" in my trigger)? The helpful message on this forum Add to Variable did mention only increasing a variable's value by 1 (SetVariable("VarName",(GetVariable("VarName") + 1)). That does not however help in solving my problem where i need the sum.

Very grateful for help. Thank you.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Mon 20 Sep 2010 04:04 PM (UTC)
Message
Quote:
(SetVariable("VarName",(GetVariable("VarName") + 1))

You know that you have %1 as the new value, so why not...
Quote:
(SetVariable("VarName",(GetVariable("VarName") + %1))

?

See this thread if you have commas in the numbers:
http://www.gammon.com.au/forum/?id=10587

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Larkin   (278 posts)  Bio
Date Reply #2 on Mon 20 Sep 2010 06:12 PM (UTC)
Message
GetVariable returns a string, so make sure you convert that to a number before doing arithmetic.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Mon 20 Sep 2010 06:14 PM (UTC)
Message
If using Lua, that conversion is done automatically in this case. But yes, you should be aware of that.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #4 on Mon 20 Sep 2010 09:26 PM (UTC)

Amended on Mon 20 Sep 2010 10:31 PM (UTC) by Nick Gammon

Message
Eloge said:

The problem is that as well ColourNote as ColourTell (and I presume, also note) send linefeeds. Can linefeeds be avoided?


Note, print, ColourNote start new lines. ColourNote and Tell don't. That is what they are for. Also you can ColourNote multiple things, eg.


ColourNote ("red", "white", "first thing ", "green", "white", "second thing")


Also string.format could help if it is all one colour. eg.


ColourNote ("green", "white", string.format ("Clio %i, Divine %i, Clio %i, Divine %i, for a total of %4.3f", 
         clio1, divine1, clio2, divine2, total))


Be aware that if you are doing "send to script" (rather than using a script file) then the % signs need to be doubled to %% in string.format.


http://www.gammon.com.au/scripts/doc.php?lua=string.format

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #5 on Tue 21 Sep 2010 01:35 AM (UTC)
Message
Eloge said:

How is 2) the arithmetic done. The values (of 306, 248, 331 and 205) are from the "%1" in my trigger)? The helpful message on this forum Add to Variable did mention only increasing a variable's value by 1 (SetVariable("VarName",(GetVariable("VarName") + 1)). That does not however help in solving my problem where i need the sum.


Stick to script variables. Saves a lot of work.

First make a function to remove the commas and convert to a number:


function fixcommas (s)
  return tonumber(string.gsub(s, ",", ""))
end


Then just do arithmetic in the "natural" way. eg.


sum = sum + fixcommas ("%1")


- 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.


21,988 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.