Undoubtedly brain-dead question

Why does the following script yield {“A”, “B”, “C”, “D”}, instead of {“A”, “C”, “D”} as expected?

set newList to {}
set oldList to {"A", "B", "C", "D"}
repeat with anItem in oldList
	if anItem ≠ "B" then set newList to newList & anItem
end repeat

Hi,

I think it has something to do with variable class coercion (although I can’t see how). This works:

set newList to {}
set oldList to {"A", "B", "C", "D"}
repeat with anItem in oldList
	log class of anItem -- already a string
	if anItem as string is not equal to "B" then set newList to newList & anItem
	--anItem as text  -- works
	--text of anItem  -- works
end repeat
return newList

Best wishes

John M

Because anItem doesn’t contain what you think it is. It’s a reference to an item in your list, not the item itself. AppleScript reference values are funny slippery things, and hard to identify if you don’t already know they’re there; many operations automatically dereference them so often they act as if they’re the referenced value itself. To get the value being referenced, you need to refer to the reference object’s contents property. e.g. Try:

set newList to {}
set oldList to {"A", "B", "C", "D"}
repeat with anItem in oldList
	if anItem's contents ≠ "B" then set newList's end to anItem -- 's contents
end repeat
newList

HTH

That’s an understatement. I’d hate to admit how long I struggled with a very much more complex version of that (which was the example I’d reduced it to eventually). I thought I had tried “as text” but I guess not. Contents seems the most generic way to go.