home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / Codeworks 0.94b3 / Codeworks® WWW Demo Doc. / Scripting Manual.doc / Scripting Manual.doc.rsrc / TEXT_146.txt < prev    next >
Encoding:
Text File  |  1995-05-01  |  2.6 KB  |  53 lines

  1. Control Structures - For
  2.  
  3.     There is another form of the for statement.  Instead of executing a block once for each number in a sequence, it executes the block once for each element of a group.  Up to now the only examples of a group that has been discussed are strings.  A string is a group of characters.  Groups are actually a more generalized object that will be discussed in a later chapter.  Everything here will apply to those groups as well.
  4.  
  5.     In a numeric for statement you specified the parameters of the numeric sequence with the receiver, to and (optionally) the by arguments.  In this for statement you specify the group of elements to iterate over, in this case a string:
  6.  
  7.         $ text, num-vowels.
  8.         text := "John Jacob Jinglehiemer-Schmidt".
  9.         num-vowels := 0.
  10.         for text do [ $ element e.
  11.             if ("aeiou" has e)
  12.                 then [ num-vowels := num-vowels + 1 ].
  13.         ].
  14.         return text.name && "has"  && num-vowels.name && "vowels".
  15.  
  16.  
  17.     Another difference from previous for statements is that instead of the index variable, there is a different variable in the iteration.  In this example, the local e will be set to each letter (or element) of the string in turn.  The index variable is also available if you want it:
  18.  
  19.         $ text, last-vowel
  20.         text := "John Jacob Jinglehiemer-Schmidt".
  21.         last-vowel := ???.
  22.         for text do [ $ element e, index i.
  23.             if ("aeiou" has e)
  24.                 then [ last-vowel := i ].
  25.         ].
  26.         if (last-vowel is-not ???)
  27.             then [ return "The last vowel is in position" && last-vowel.name ]
  28.             else [ return "There were no vowels" ].
  29.  
  30.  
  31. ¬ª    By now, you know enough about scripts to recognize that declaration expression in the for statement‚Äôs block looks exactly like argument declarations for a script.  In fact they are argument declarations.  The for statement passes arguments to the block, and element and index are block arguments.
  32.  
  33.  
  34. Reverse For
  35.     This form of for statement has an optional argument reverse.  If you specify reverse, then the elements (and the index) are iterated from the end to the start:
  36.  
  37.         $ text, rev-text.
  38.         text := "college".
  39.         rev-text := "".
  40.         for text reverse do [ $ element e.
  41.             rev-text := rev-text & e.
  42.         ].
  43.         return rev-text.name && "is" && text.name && "spelt backwards".
  44.  
  45.  
  46. ¬ª    Notice that the reverse argument doesn‚Äôt take any value, you just add it to the statement.  This is an example of a flag argument, which is discussed in more detail later.
  47.  
  48. ¬ª    Numeric for statements don‚Äôt use reverse.  Instead you just use a negative by argument: 
  49.  
  50.         for 10 to 1 by -1 do [ $ index t.
  51.             transcript put "T minus" && t.name & ", and counting‚Ķ".
  52.         ].
  53.         transcript put "Liftoff!".