Integer comparison in repeat loops
Integer comparison in repeat loops
- Subject: Integer comparison in repeat loops
- From: Richard Clark <email@hidden>
- Date: 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
On changing this to:
(2)
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 as integer) = 0 then
display dialog "Yes, r is 0"
else
display dialog "No, r isn't 0"
end if
end repeat
the comparison works.
Changing the test in (1) to either:
r < 1 -- catches the 0
or,
r > -0.9999 -- obviously should catch everything
will compare correctly.
Obvious workaround is to explicitly declare r as an integer.
Possibly has something to do with the structure of the repeat loop as
defining the loop with:
(3)
set rgbDecimalTriplet to {65024, 0, 13097}
repeat with rgbValue from 1 to (count rgbDecimalTriplet)
if item rgbValue of rgbDecimalTriplet = 0 then
display dialog ("Item " & (rgbValue as string) & ": " & (item
rgbValue of rgbDecimalTriplet) as string) & " is 0"
end if
end repeat
works just fine. Just curious why this is so.
0 = 0
or
0.0 = 0
both evaluate to true.
Tools used for the above:
Mac 8500, OS 9.1, Script Editor 1.83 or Smile 1.8.8, AppleScript Extension
1.83
Note: Hard returns inserted on long script lines above.
-Richard
_______________________________________________
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.