Can someone explain to me why “A” = “A” in one situation but not the other
property mainList : {"A", "B", "Q"}
log "----> calling searchItem()"
searchItem(item 1 of {"A", "Q"}, 1)
log "----> result 1: " & result
log "----> calling searchItemsInList()"
searchItemsInList({"A", "Q"})
log "----> result 2: " & result
on searchItemsInList(theList) -- return true if all items in theList are in the mainList
repeat with theItem in theList
if searchItem(theItem, 1) is false then return false
end repeat
return true
end searchItemsInList
on searchItem(theItem, startIndex) -- return true if theItem is in the mainlList
repeat with i from startIndex to (count of mainList)
log "test: " & (item i of mainList) & " = " & theItem
if (item i of mainList) = theItem then
log "test result: true"
return true
end if
log "test result: false"
end repeat
return false
end searchItem
-- log output ------
(*----> calling searchItem()*)
(*test: A = A*)
(*test result: true*)
(*----> result 1: true*)
(*----> calling searchItemsInList()*)
(*test: A = A*)
(*test result: false*)
(*test: B = A*)
(*test result: false*)
(*test: Q = A*)
(*test result: false*)
(*----> result 2: false*)
-- So
repeat with i from 1 to 2
return item i of {"a", "b"}
-- returns the item
end repeat
-- while
repeat with theItem in {"a", "b"}
return theItem
-- returns a reference to the item
-- that automagically gets printed as the contents
-- by the log command
end repeat
--I guess what threw me was that the following is also true
set aRef to a reference to {"a", "b"}
set n to count of aRef --> 2 (a count of items in the list, not the reference)
set i to item 1 of aRef --> "a" (again no need for 'contents')
I guess references to values are handled differently than references to data structures
The key here is that in many instances, AppleScript dereferences for you:
set D to {"A", "B"}
set asc to {}
repeat with C in D
set asc's end to ASCII number of C
end repeat
asc --> {65, 66}
But it does NOT do so in boolean tests:
set D to {"A", "A"}
set T to "A"
set tst to {}
repeat with C in D
if T = C then
set tst's end to true
else
set tst's end to false
end if
end repeat
tst --> {false, false}
I should add that for this discussion, is not and is not equal to might be called “negative equality” tests rather than “inequality” tests. Dereferencing doesn’t take place with these:
set D to {"A", "A"}
set T to a reference to item 1 of D
set tst to {}
repeat with C in D
set tst's end to (C is not equal to T)
end repeat
tst --> {false, true}
But with the compound operators is less than or equal to and is greater than or equal to, dereferencing takes place for both the equality and inequality components. I suspect that beneath the surface, these operators are executed as their “negative inequality” equivalents: is not greater than and is not less than.
set D to {"A", "B", "A"}
set T to a reference to item 1 of D
set tst to {}
repeat with C in D
set tst's end to (C is less than or equal to T) -- = (C is not greater than T)
end repeat
tst --> {true, false, true}