[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Simple arithmethics, adding values
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Simple arithmethics, adding values

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


Posted by Eloge   (20 posts)  [Biography] 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.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Larkin   (278 posts)  [Biography] 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.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] 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.


4,977 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]