Simple arithmethics, adding values

Posted by Eloge on Mon 20 Sep 2010 02:48 PM — 6 posts, 29,747 views.

#0
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.
USA #1
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
#2
GetVariable returns a string, so make sure you convert that to a number before doing arithmetic.
USA #3
If using Lua, that conversion is done automatically in this case. But yes, you should be aware of that.
Australia Forum Administrator #4
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
Amended on Mon 20 Sep 2010 10:31 PM by Nick Gammon
Australia Forum Administrator #5
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")