# Counting Repeats

Is there some way to count the number of times a repeat statement actually repeats?

I had a feeling that there must be some way of counting this since you can set it to repeat a number of times so it has to know when to stop.

Any help would be great.

I can’t think of an example, but you could do something like this:

``````set repeatCounter to 0

--repeat (whatever)
set repeatCounter to repeatCounter + 1

--do stuff
--end repeat

return repeatCounter

``````

Simon:

I think the easiest way is to use the repeat variable itself. For instance, if you have a list of items you are working on, but you don’t know exactly how large the list is, you simply use this statement:

``````repeat with x from 1 to (count the_list)
--do something with x
end
``````

As the loop goes along, x continues to increase by increments of 1, until it reaches the end of the_list. Once the loop is over, you can just grab x, and it should be the same as the number of items in the_list.

If you are trying to see how far a loop gets before an error occurs, I think using a second variable would be helpful, like this:

``````repeat with x from 1 to (count the_list)
set y to x
--do something with x
end
``````

When the error occurs, you now have a second variable, y, that should tell you how far you made it before the crash.

I admit that I am still a novice scripter, so this may not be what you are looking for. I know that there are better ways to trap and identify errors; the Applescript book by Hanaan Rosenthal has some excellent ideas on trapping errors in your scripts.

Good luck, and be sure to remember that this forum works best if you are very clear about you want to do, espcecially if you can provide some sample code that you are struggling with.

casdvm

.:From Simon:.

Those scripts that you gave me don’t seem to be working. It’s probably my fault but here is the script I am trying to use it in. It is a simple game where the user has to guess the random number. I wuld like to be able to count the number of times that the users puts in a guess up until the final dialog box. The random number could be anywhere between and including 0 and 100.

``````
set theNumber to (random number 100)
repeat
set theGuess to the text returned of (display dialog "Enter guess:" default answer "50") as integer
if theGuess is greater than theNumber then display dialog "You will need to guess lower."
if theGuess is less than theNumber then display dialog "You will need to guess higher."
if theGuess is equal to theNumber then display dialog "Well done!  You managed to guess the number which is " & theNumber & ".  Click Cancel to stop"
end repeat
``````

Hi Simon,

Something like guardian34’s suggestion would give:

``````set theNumber to (random number 100)
set x to 1 -- set the variable
repeat
set theGuess to the text returned of (display dialog "Enter guess:" default answer "50") as integer
if theGuess is greater than theNumber then display dialog "You will need to guess lower."
if theGuess is less than theNumber then display dialog "You will need to guess higher."
if theGuess is equal to theNumber then display dialog "Well done!  You managed to guess the number which is " & theNumber & " in " & x & " tries.  Click Cancel to stop"
set x to x + 1 -- add one each repeat of the loop
end repeat
``````

Best wishes

John M

Simon:

This now works:

``````global theCounter, theNumber
set theCounter to 0
set theNumber to (random number 100)
repeat
set theGuess to the text returned of (display dialog "Enter guess:" default answer "50") as integer
set theCounter to theCounter + 1
if theGuess is greater than theNumber then display dialog "You will need to guess lower."
if theGuess is less than theNumber then display dialog "You will need to guess higher."
if theGuess is equal to theNumber then ReStart()
end repeat
on ReStart()
display dialog "Well done!  You managed to guess the number which is " & theNumber & " in " & theCounter & " guesses.  Click Cancel to stop"

set theCounter to 0
end ReStart
``````

The main issue I had was the whole automatic restart thing. There was no way to reset the counter to 0 once the loop restartedd after the first correct guess. So, I put the ending and variable reset into a handler.

Hope this helps.

casdvm

Simon:

I just noticed that even though the repeat continues along, it never gets a new random number. I added that line to the handler, and now you can guess all day long with new numbers all the time:

``````on ReStart()
display dialog "Well done!  You managed to guess the number which is " & theNumber & " in " & theCounter & " guesses.  Click Cancel to stop"
set theCounter to 0
set theNumber to (random number 100)
end ReStart
``````

casdvm

Simon:

``````repeat
set guess_count to 0
set the_number to random number 100
set the_text to "Guess my number between 1 and 100:"
repeat
set the_guess to text returned of (display dialog the_text default answer "50")
set guess_count to guess_count + 1
if the_number is less than the_guess then
set the_text to "You will need to guess lower."
else if the_number is greater than the_guess then
set the_text to "You will need to guess higher."
else
display dialog "Well done! You managed to guess the number which is " & the_number & " in " & guess_count & " guesses. Go again?"
exit repeat
end if
end repeat
end repeat
``````