Failing comparison

Hi,

I would expect the third comparison to be false but the comparison is always true? Why?

property notAvailable : "N/A"
set x to {1, 2, notAvailable}
repeat with ix in x
	display dialog ix
	if ix is not notAvailable then
		display dialog "if: " & ix
	end if
end repeat

In your repeat loop, “ix” is a reference to an item in a list, not the actual item itself. You need to coerce it into some item type or class, so it knows explicitly how to compare the two. Replacing…

if ix is not notAvailable then

… with …

if (ix as string) is not (notAvailable as string) then

… works for me.

j

That is right. Here is another short thread that may help: http://bbs.applescript.net/viewtopic.php?id=13756

For what it’s worth, here is a modification of your script that works:

property notAvailable : "N/A"
set x to {1, 2, notAvailable}
repeat with ix from 1 to count x
	display dialog (item ix of x)
	if item ix of x is not notAvailable then
		display dialog "if: " & item ix of x
	end if
end repeat

To get the value of the reference for comparison, use ‘contents of’:

property notAvailable : “N/A”
set x to {1, 2, notAvailable}
repeat with ix in x
display dialog ix
if (contents of ix) is not notAvailable then
display dialog "if: " & ix
end if
end repeat

gl,

Interestingly (to me anyway), contents of anItem is necessary when dealing with a repeat with anItem in listOfItems, but doesn’t seem to be necessary in repeat with k from 1 to count of listOfItems structures where the coercion seems to happen without explicit contents of.

Adam Bell, could you please refrain from posting using the color blue to highlight text? You’re using the same color to emphasize parts of your comments as the forum’s default link color. This is very confusing, and may lead some people to miss key information in links that people add inline in their posts. Your previous post has a handful of colorized sections that I assumed from their color were links. Using single or double quotes, bold, italic, or at least a different color not found in the forum would be more appropriate.

j

Hi Adam,

I think they did this to get a fast repeat loop.

repeat with this_item in some_list
– do something with this_item
end repeat

It’s set a compile time, so instead of looking up the variable some_list, it knows where it already is. Think I read that somewhere.

gl,