Re: Skip a step in a repeat loop
Re: Skip a step in a repeat loop
- Subject: Re: Skip a step in a repeat loop
- From: Nigel Garvey <email@hidden>
- Date: Thu, 30 Aug 2001 22:40:30 +0100
Arthur J Knapp wrote on Thu, 30 Aug 2001 13:37:19 -0400:
>
> From: Paul Berkowitz <email@hidden>
>
> Somebody here (I forget whom - perhaps Nigel will remember) [posted a
>
really
>
> brilliant workaround - nay, solution - to this problem. I use it all the
>
> time. As you say, the if/then stuff gets horribly messy, especially when
>
> you're trying to exit long before the end of the repeat loop. Here:
>
>
> repeat with i from 1 to 100 -- or whatever, any sort of repeat loop
>
>
>
> repeat 1 times
>
>
>
> --blah blah
>
> if something "what you want" then
>
> exit repeat --goes on to next iteratio
>
> end if
>
> --lots of blah blah
>
> --tons of scripting
>
> end repeat
>
> end repeat
>
>
I actually think that this may have been me, though I'm not sure.
I thought it was Arthur too, but I wasn't sure either. It's the sort of
thing he would have invented if he'd thought of it first. ;-) So I've
just done a text search through my personal archive and the earliest
mention I have of it is by.... Ray Robertson, who presented the idea to
this list for comment on 27th June 2000.
[...]
>
Lately, I've just been putting in a single repeat immediately within the
>
outer repeat. This gives me the option to return to the top at any time by
>
using an exit repeat--a virtual "skip to next item". If none of the
>
conditions lead to an exit repeat, then the "repeat 1 time" construct just
>
results in going to the next item at the bottom of the repeat.
>
>
This is especially handy in watching for various errors, or testing for
>
false/problem conditions.
>
>
Example...
>
>
repeat with i in theList
>
repeat 1 times
>
if not A
>
exit repeat
>
end if
>
**lots of code here
>
try
>
**code here
>
on error
>
--report/record specific error here
>
exit repeat
>
end try
>
if B...
>
**code here
>
else
>
exit repeat
>
end if
>
try
>
**code here
>
on error
>
--report/record another specific error here
>
exit repeat
>
end try
>
end repeat
>
end repeat
NG