Determining a response from a variable number

Posted by Lessthanluminous on Fri 16 Jul 2010 06:47 AM — 8 posts, 30,352 views.

#0
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.
Amended on Fri 16 Jul 2010 07:23 AM by Lessthanluminous
USA #1
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!
Amended on Fri 16 Jul 2010 08:08 AM by Twisol
USA #2
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
Australia Forum Administrator #3
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


USA #4
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.
Amended on Fri 16 Jul 2010 10:20 AM by Twisol
Australia Forum Administrator #5
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.
Amended on Fri 16 Jul 2010 10:48 AM by Nick Gammon
Australia Forum Administrator #6
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"


Amended on Fri 16 Jul 2010 10:51 AM by Nick Gammon
#7
Thanks! This looks great and I will give it a shot!