MattJayC

Q: 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

Close

Q: If in a range of numbers

  • All replies
  • Helpful answers

  • by MattJayC,

    MattJayC MattJayC Aug 24, 2016 5:36 AM in response to MattJayC
    Level 1 (5 points)
    Mac OS X
    Aug 24, 2016 5:36 AM in response to 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 aw then

    Display Dialog "In the Range!"

    end if

  • by Bernard Harte,Helpful

    Bernard Harte Bernard Harte Aug 24, 2016 6:05 AM in response to MattJayC
    Level 4 (3,309 points)
    iPhone
    Aug 24, 2016 6:05 AM in response to MattJayC

    '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

  • by VikingOSX,Helpful

    VikingOSX VikingOSX Aug 24, 2016 6:05 AM in response to MattJayC
    Level 7 (20,606 points)
    Mac OS X
    Aug 24, 2016 6:05 AM in response to MattJayC

    Or, 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!"

  • by Hiroto,

    Hiroto Hiroto Aug 24, 2016 10:05 AM in response to MattJayC
    Level 5 (7,281 points)
    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