DekGenius.com
Team LiB   Previous Section   Next Section
repeat with {loop variable} from {integer} to {integer}[by stepVal] end [repeat]

Syntax

Repeat with loopVar from 1 to 10
   (* code statements *)
end repeat

Description

This form of the repeat loop executes a specified number of times over a range of values. A loop variable keeps track of how far the repeat loop has progressed in cycling over its range of loops. The loop variable increments by the value of stepVal (or one by default if the stepVal variable is not specified) throughout each loop. This makes the repeat with statement much more flexible and powerful than repeat {integer} times. You can take the value of the loop variable and use it in the executing code, as in the following example. Once this repeat with statement reaches the end of its range, as in:

repeat with loopVar from 1 to 10

(10 is the end of the range here), then the repeat loop terminates and code execution resumes with the statement following end repeat. You can also use the exit statement to terminate this loop (see "exit"). repeat with is similar to the famous:

for (i=0; i < rangeVar; i++)

variation of the loop statement that JavaScript, Java, and C++ programmers are very familiar with.

Examples

This AppleScript loops through each character of a word to see if any character is repeated. It uses the loop variable to determine which character in the word to examine. This example also shows how you can specify any of the range values with expressions that return integers, instead of just literal integers:

repeat with loopVar from 2 to (2^2)
   set theString to the text returned of (display dialog¬
   "Enter a word and I'll tell you which letter, if any, repeats first"¬
   default answer "")
   set len to (length of theString) (* len is set to the number of
characters in string *)
   tell application "BBEdit 5.0"
      repeat with loopVar from 1 to len (* repeat from char 1 to length
of string *)
         if loopVar is equal to 1 then set charList to {} (*create a list 
to hold the examined characters *)
         set tempChar to (character loopVar of theString) (* tempChar is a 
single character in the string like 'o' *)
         if tempChar is in charList then (* if it's already in the list 
         then it appears more than once in the string *) 
            display dialog  "Your repeating character is " & tempChar
            exit repeat (* exits the repeat loop; finishes executing the 
            script *)
         end if
         set charList to charList & tempChar (* no repeating chars yet so 
add the current char to the list *)
         if loopVar is equal to len then (* if this is true then we did not 
find any repeaters *)
            display dialog "You had no repeating characters!"
         end if
      end repeat
   end tell
end repeat

This script uses the BBEdit text editor because this app is good at examining text. The script gets a word from the user using the display dialog scripting addition (Part IV of the book discusses scripting additions). Then it uses a repeat with loop to get each single character in the string and store it in a variable of type list (i.e., charList). This is how the script keeps track of the characters it has already examined. The loopVar of the repeat with statement identifies individual characters of the string with an index reference form, as in character loopVar. If loopVar were 3 then the expression would evaluate to character 3, which is the third character in the string. The code then checks the charList list of characters to see if the currently examined character is already in there. If the character is already in the list then it appears more than once in the string. Then the script tells the user which character repeated and exits the loop:

display dialog "Your repeating character is " & tempChar

This example shows this repeat with loop with a specified step value:

set theString to "Kindly give me every other word."
set allWd to words of theString -- returns list of words
set len to length of allWd
set userMsg to ""
repeat with indx from 1 to len by 2 -- repeat loops over the list by two
   set userMsg to userMsg & return & (item indx of allWd)
end repeat
display dialog "Here's every other word on its own line: " & return &¬ userMsg
    Team LiB   Previous Section   Next Section