Re: Integer comparison in repeat loops
Re: Integer comparison in repeat loops
- Subject: Re: Integer comparison in repeat loops
- From: Nigel Garvey <email@hidden>
- Date: Sat, 17 May 2003 11:19:29 +0100
Richard Clark wrote on Fri, 16 May 2003 18:13:50 -0400:
>
While writing a little handler to transform an RGB decimal triplet to hex I
>
came across somewhat odd behaviour of repeat loop integer comparison as the
>
following example shows:
>
>
(1)
>
>
set rgbDecimalTriplet to {65024, 0, 13097}
>
>
repeat with r in rgbDecimalTriplet
>
display dialog "class of " & (r as string) & " " & "is " & (class of r
>
as string)
>
if r = 0 then
>
display dialog "Yes, r is 0"
>
else
>
display dialog "No, r isn't 0"
>
end if
>
end repeat
With this particular kind of repeat loop ('repeat with this in that'),
the value of the loop variable is not the item itself, but a "reference"
to it. In other words, the value of r first time round your loop is not
the integer 65024, but the reference 'item 1 of {65024, 0, 13097}'. In
most cases this can be ignored, as the reference will automatically be
resolved to make sense when doing maths, getting properties or items,
coercing, or comparing relative sizes. But when testing for equality (is,
=, is equal to, is not equal to) it does matter because r is one kind of
object and the item in the list is another, so they're never equal.
The standard way to resolve a reference is with the 'contents' operator:
if contents of r is 0 then
-- etc.
>
if (r as integer) = 0 then
This also works in your case, but only because you know that all the
items in the list are integers anyway. It would also test true if the
list item were the string "0.0" and would error if the list item couldn't
be coerced to an integer.
NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.