If in a range of numbers

I'm sure this is really simple. but I can't find the words to search for it to find my answer.


set w to "19"

if w is in {1 thru to w} then

display dialog "in the range!"

end if


Many Thanks

Matt

iMac, OS X Mavericks (10.9.4)

Posted on Aug 24, 2016 3:32 AM

Reply
4 replies

Aug 24, 2016 10:05 AM in response to MattJayC

Hello


Be careful about the class of values in comparision. E.g.,



"9" ≤ "19" -- false "9" ≤ 19 -- equivalent to "9" <= "19"; false 9 ≤ "19" -- equivalent to 9 <= 19; true 9 ≤ 19 -- true



So, if understand it correctly, your script should have been something like this.



set a to "9" set w to "19" if (0 + a) ≥ 1 and (0 + a) ≤ w then display dialog "In the Range!" end if




Also you may define a handler for range test as follows.



set w to "19" set a to "9" if in_range(0 + a, {1, w}) then display dialog "" & a & " is in range [1, " & w & "]" else display dialog "" & a & " is out of range [1, " & w & "]" end if on in_range(x, {a, b}) (* number or string x : source value number or string a, b : range boundaries, inclusive return boolean : true if x in [a, b], false otherwise * comparisons are based upon class of x e.g., if x is number, x >= a and x <= b are numerical comparisons if x is string, x >= a and x <= b are lexical comparisons *) x ≥ a and x ≤ b end in_range




Regards,

H

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

If in a range of numbers

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.