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
➜ General
➜ 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 page
Posted by
| Lessthanluminous
(9 posts) 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. | Top |
|
Posted by
| Twisol
USA (2,257 posts) 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 | Top |
|
Posted by
| WillFa
USA (525 posts) 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
| Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) 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 | Top |
|
Posted by
| Twisol
USA (2,257 posts) 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 | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) 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 | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) 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 | Top |
|
Posted by
| Lessthanluminous
(9 posts) 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! | 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.
22,697 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top