Re: Silly question?
Re: Silly question?
- Subject: Re: Silly question?
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 19 Aug 2003 19:22:40 -0700
On 8/19/03 6:25 PM, "Glenn" <email@hidden> wrote:
>
Why the heck does:
>
>
tell application "Microsoft Entourage"
>
repeat with the_contact in contacts
>
display name of the_contact
>
end repeat
>
end tell
>
>
Take *forever* to display each contact name, while:
>
>
tell application "Microsoft Entourage"
>
repeat with contact_index from 1 to count contacts
>
display name of contact contact_index
>
end repeat
>
end tell
>
>
Just blazes along?(?)
I haven't noticed such a great difference myself, but in the first case,
you're forcing the script to evaluate
item 1 of {contact id 1 of application "Microsoft Entourage", contact id
2 of application "Microsoft Entourage", contact id 6 of application
"Microsoft Entourage", .. etc, etc. etc.}
item 2 of {contact id 1 of application "Microsoft Entourage", contact id
2 of application "Microsoft Entourage", contact id 6 of application
"Microsoft Entourage", .. etc, etc. etc.}
...
etc. etc. etc.
It has to fetch the entire list every time.
In the second case it just goes straight for the correct item . In
AppleScript, unlike other languages, you avoid a lot of problems by not
using 'repeat with var in aList' and instead use your second method. (Most
people use "i" instead of "contact_Index"). Aside from the performance
issue, you will get unexpected results (false negatives, as far as you're
concerned) when you use the equality operator ( = ).
set aList to {1, 2, 3}
repeat with anItem in aList
if anItem = 1 then
display dialog "Yes"
return
end if
end repeat
display dialog "No" -- only if not successful
(In the above case you'd need to state
if contents of anItem = 1
to get what you want since
item 1 of {1, 2, 3} -- a reference
is not precisely
1 -- an integer
since their classes (reference vs. integer) are different.
. On the other hand this:
set aList to {1, 2, 3}
repeat with with i from 1 to (count aList)
set anItem to item i of aList
if anItem = 1 then
display dialog "Yes"
return
end if
end repeat
display dialog "No" -- only if not successful
always works because setting a variable (anItem) to the reference (item 1 of
{1, 2, 3}) resolves the reference to its value (1).
--
Paul Berkowitz
_______________________________________________
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.