Re: Simple if .. else .. then statement
Re: Simple if .. else .. then statement
- Subject: Re: Simple if .. else .. then statement
- From: Paul Berkowitz <email@hidden>
- Date: Sat, 29 May 2004 11:57:39 -0700
On 5/29/04 11:17 AM, "John Hawkinson" <email@hidden> wrote:
>
Paul Berkowitz <email@hidden> wrote on Sat, 29 May 2004
>
at 09:29:18 -0700 in <BCDE086E.663EDžemail@hidden>:
>
>
Question, if I may. I feel like it's a dumb quesiton, which is why
>
I didn't copy the list, but feel free to reply there if you think
>
you should...
>
>
> repeat with i from 1 to (count aList)
>
> set anItem to item i of aList
>
>
It seems a lot of people favor this syntax over
>
this other one:
>
>
repeat with anitem in aList
>
>
is there some reason? Like is counting explicitly faster or something?
>
It seems peculiar to go through the extra effort to count the list and
>
set a variable to it...
It's not a dumb question. The most important reason why I almost always use
this repeat loop syntax is because there are many circumstances (but not
always easy to know which circumstances, so I just use this syntax all the
time to be safe) where the other syntax will not work as you expect. In
particular, using the equality operator ('='):
repeat with anitem in aList
if anItem = "a" then
will not get you the result you want. If aList = {"a", "b", "c"} then
anItem is actually
item 1 of {"a", "b", "c"}
NOT "a". So
if anItem = "a"
will be _false_, not true. (The equality operator is obsessively punctilious
and precise.)
You'd have to use:
if contents of anItem = "a"
I happen to find that much harder to remember than using the other syntax by
habit (and you can occasionally come adrift if anItem happens to be a record
with a 'contents' property). By using 'repeat with i from 1 to (count
aList)', I never, ever run into that issue.
Then, I usually do use methods for speeding up list iteration for long
lists. These work tremendously, but are another topic. In short, they use
'my aList' if at the top level of a script or aList is a global variable or
script property, or "Serge" script objects if in a handler. In both cases,
there is a way of iterating using the 'repeat with anItem' syntax, but again
I find them more cumbersome than using the 'set anItem to item i of my
aList' or 'set anItem to item i of sergeScript's listProperty'.
--
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.