Looping thru items in a list: two loop controls only one works?

Hi,
I am attempting to loop through the items in a list which is a list of strings. In the two loops shown below both run and read values from the list but only the second returns the string directly. In the first loop the variable tEventText is set to what appears to be a command e.g. item 2 of {"stringA","stringB","stringC"}
Whereas the second version that uses the index i resolves the string value directly. Is this an artefact of looking at the variables using ScriptDebugger or is this what AppleScript does?

###### First loop appears to return a command that gets processed when used ######
repeat with tEventText in tTodaysEvents
	set tTextColour to my GetEventTextColour(tEventText)
	set end of tText to {font:pfont, size:pCalEventTextSize, color:tTextColour, alignment:left, text:tEventText}
end repeat
###### This loop returns the string ######
set tEventCount to count of tTodaysEvents
repeat with i from 1 to tEventCount
	set tEventText to item i of tTodaysEvents
	set tTextColour to my GetEventTextColour(tEventText)
	set end of tText to {font:pfont, size:pCalEventTextSize, color:tTextColour, alignment:left, text:tEventText}
end repeat

Both versions appear to run at similar speed with short lists but in my opinion debugging is simplified by using the second form.

You’ve stumbled across a known AppleScript weirdness when using the repeat with anItem in someList form - the loop variable will actually be a reference to an item in the list. This is not normally dereferenced unless you explicitly coerce it or use it in some situation that involves an automatic coercion, such as appending it to a string.

A typical problem that occurs is when doing something like comparing the loop variable to a string, which will fail because the reference is not a string.

Yes that is what happened: it took a little while to work out what was happening.
S

An alternative to coercion is to use the contents operator:

repeat with tEventText in tTodaysEvents
	set tEventText to tEventText's contents
	set tTextColour to my GetEventTextColour(tEventText)
	set end of tText to {font:pfont, size:pCalEventTextSize, color:tTextColour, alignment:left, text:tEventText}
end repeat

Thanks again, I’m new to lists and ‘contents’ is one that I shall have to remember.

I wrote above that both loops were about equal in speed but I now think that using index i is faster.

S

Keep in mind that this weirdness is actually a feature. In some cases working with a reference to the item is faster and can be more efficient than getting the item by index, or coercing the item to its contents.