Re: Re: Repeat With question (newbie level)
Re: Re: Repeat With question (newbie level)
- Subject: Re: Re: Repeat With question (newbie level)
- From: John W Baxter <email@hidden>
- Date: Wed, 14 Feb 2001 20:21:18 -0800
At 18:11 -0800 2/14/01, Michelle Steiner wrote:
>
On 2/14/01 5:52 PM, email@hidden <email@hidden> wrote:
>
>
>There is a caveat with this approach (which is a correct and very good
>
>approach BTW, being standard Applescript and all). A script will demonstrate:
>
>
>
>set alphaList to {"a", "b", "c"}
>
>
>
>repeat with aLetter in alphaList
>
> if aLetter is equal to "b" then beep 1
>
> if aLetter is "b" then beep 1
>
> if aLetter = "b" then beep 1
>
> if aLetter is equal to "b" then beep 1
>
> if (aLetter as string) is equal to "b" then beep 1 -- only this line
>
>makes a beep
>
>end repeat
>
>
This is true, but I don't understand why. Look at this:
>
>
set alphaList to {"a", "b", "c"}
>
>
repeat with aLetter in alphaList
>
if aLetter is equal to "b" then beep 1
>
if aLetter is "b" then beep 1
>
if aLetter = "b" then beep 1
>
if aLetter is equal to "b" then beep 1
>
if (aLetter as string) is equal to "b" then beep 1 -- only this line
>
makes a beep
>
end repeat
We've done this before. The variable aLetter does not become in turn "a"
then "b" then "c" as it appears it should. Instead, it becomes
item 1 of alphaList
then item 2 of alphaList
then item 3 of alphaList
We need to turn those into the actual letter. In the above code, only the
as string
does that job.
We can change the script just a little and it will be a lot noisier:
set alphaList to {"a", "b", "c"}
repeat with aLetterItem in alphaList
set aLetter to contents of aLetterItem
if aLetter is equal to "b" then beep 1
if aLetter is "b" then beep 1
if aLetter = "b" then beep 1
if aLetter is equal to "b" then beep 1
if (aLetter as string) is equal to "b" then beep 1
end repeat
Now, aLetter really is a one letter string each time.
--John
--
John Baxter email@hidden Port Ludlow, WA, USA