-
All replies
-
Helpful answers
-
Aug 24, 2016 5:36 AM in response to MattJayCby MattJayC,I still did't ask the question right.
set a to "15"
set w to "19"
if w is in {1 thru to w} then
display dialog "in the range!"
end if
Gone for a work around.
if a ≥ 1 and a ≤ w then
Display Dialog "In the Range!"
end if
-
Aug 24, 2016 6:05 AM in response to MattJayCby Bernard Harte,★Helpful'Contains' is what you're looking for.
However, if the range is contiguous I think your 'workaround' is better.
Here's an example that populates the list from 1-19 and then does the check:
set theList to {}
set a to 15
repeat with w from 1 to 19
set end of theList to w
end repeat
theList contains a
-
Aug 24, 2016 6:05 AM in response to MattJayCby VikingOSX,★HelpfulOr, the following:
set myRange to {}
set w to "19"
set a to "15"
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set myRange to text items of (do shell script "echo $(seq 19)")
set AppleScript's text item delimiters to TID
if a is in myRange then display dialog "In the range!"
-
Aug 24, 2016 10:05 AM in response to MattJayCby Hiroto,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_rangeRegards,
H