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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Determining a response from a variable number

Determining a response from a variable number

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


Posted by Lessthanluminous   (9 posts)  [Biography] bio
Date Fri 16 Jul 2010 06:47 AM (UTC)

Amended on Fri 16 Jul 2010 07:23 AM (UTC) by Lessthanluminous

Message
If a variable is set to a number that changes often, how would I go about a trigger sending AAA if a variable is under 10, send BBB (or nothing) is the number is between 10 and 20, and CCC if it is over 20?

I suppose I could do a lot of elseif's and/or make a LOT of variables with one very long trigger to set each one, but I'm almost sure there is a way to do some form of 'greater than, less than'.

Also, (less important) could I compare this to another variable so that less than 10 would send AAA or aaa, depending on what the other one was?

Ideally something simple, but I will work with what I get!

Thanks in advance~

And I just realized I misspelled response. Good job for me.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #1 on Fri 16 Jul 2010 08:07 AM (UTC)

Amended on Fri 16 Jul 2010 08:08 AM (UTC) by Twisol

Message
If a programming language has anything but a full complement of comparison operators, it's not a very good language! ;) You haven't told us what language you're using, but it's pretty much universal that "x < 10" would evaluate to true if X is less than 10, and false if X is greater than or equal to 10. Then you have <= which is less-than-or-equals, >, and >=. Most languages use == for "exactly equal", though VBscript oddly uses =.

Here's an example of what you want in Lua:

x = 15 -- example
if x < 10 then
  Send("AAA")
elseif x >= 10 or x <= 20 then
  Send("BBB")
else
  Send("CCC")
end


I don't need to use "x > 20" in the last branch, because if you look at it logically, if the two previous conditions failed then it -must- be greater than 20 anyways.

If you're not using Lua, or if you're doing something else differnt, let us know!

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by WillFa   USA  (525 posts)  [Biography] bio
Date Reply #2 on Fri 16 Jul 2010 08:47 AM (UTC)
Message
Twisol said:

x = 15 -- example
if x < 10 then
  Send("AAA")
elseif x >= 10 or x <= 20 then
  Send("BBB")
else
  Send("CCC")
end



I've seen you do this a few times now. Specificly testing that x >= 10 is redundant, you're in the elseif branch. Lua isn't like C where you need to break out of a switch's case statement.


if tonumber(x) ~= x then return end --just to be sure we have a number.
if x < 10 then
  Send("AAA")
elseif x < 20 then
  Send("BBB")
else
  Send("CCC")
end
[Go to top] top

Posted by Nick Gammon   Australia  (23,001 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Fri 16 Jul 2010 10:02 AM (UTC)
Message
Twisol said:


x = 15 -- example
if x < 10 then
  Send("AAA")
elseif x >= 10 or x <= 20 then
  Send("BBB")
else
  Send("CCC")
end




You mean:


elseif x >= 10 and x <= 20 then


Apart from the problem raised that you don't need to test for being >= 10 at all.

Otherwise a number like 30 will pass the first test (x >= 10) and the second test won't be done.

Willfa said:

if tonumber(x) ~= x then return end --just to be sure we have a number.


Maybe:

if type (x) ~= "string" then return end -- make sure we have a number


Or:


x = tonumber (x)  -- convert string to number
if not x then return end -- impossible? give up



- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #4 on Fri 16 Jul 2010 10:15 AM (UTC)

Amended on Fri 16 Jul 2010 10:20 AM (UTC) by Twisol

Message
It's late! :(

WillFa said:
I've seen you do this a few times now.

Huh. Where else? I know there was a misunderstanding about the inverse of a boolean expression, but that was cleaned up so quick there's nothing left to refer back to.


Aaand.... I'm not sure why my snippet was compared to a switch statement. Also, your example uses <20 instead of <=20, which isn't precisely equivalent to my example. It wasn't clear which one they wanted, but still, there's no reason to make things muddled.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (23,001 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Fri 16 Jul 2010 10:45 AM (UTC)

Amended on Fri 16 Jul 2010 10:48 AM (UTC) by Nick Gammon

Message
At the risk of confusing the OP with all this talk about booleans and switch statements, if he really meant send BBB "between 10 and 20" then it should read:


if x < 10 then
  Send("AAA")
elseif x <= 20 then
  Send("BBB")
else
  Send("CCC")
end


Just to be absolutely clear, it would send AAA for numbers from 0 to 9 (or indeed any number less than 10, so -4342334 would cause it to be sent too). Of course the matching wildcard may not pick up negative numbers, so you may not need to worry about that.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (23,001 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Fri 16 Jul 2010 10:50 AM (UTC)

Amended on Fri 16 Jul 2010 10:51 AM (UTC) by Nick Gammon

Message
As Willfa said, you may want to test the sanity of x (eg. is it a number, is it negative, is it 23429498454?). That's what the Marx Brothers called the Sanity Clause.

Quote:

Groucho: "That's in every contract, that's what you call a sanity clause."

Chico: "You can't a fool a me there ain't no sanity clause"



- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Lessthanluminous   (9 posts)  [Biography] bio
Date Reply #7 on Fri 16 Jul 2010 03:35 PM (UTC)
Message
Thanks! This looks great and I will give it a shot!
[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.


19,756 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 HostDash]