|
|||||
|
|||||
Looping constructs |
OmniMark provides five types of looping constructs, repeat
, repeat for
, repeat to
, repeat over
, and repeat scan
.
You can use a repeat
loop to perform a series of actions repeatedly. You must explicitly exit a repeat
loop using either a throw
or an exit
:
process local integer foo repeat output "foo is %d(foo)%n" increment foo exit when foo = 4 again
This program will create the following output:
foo is 0 foo is 1 foo is 2 foo is 3
You can use a repeat to
to repeat a block of code a specific number of times. The loop will repeat the number of times specified by an integer expression following the to
:
process repeat to 5 output "X" again
You can use a repeat for
loop to repeat over a block of code a specific number of times. The for loop provides a control variable for the loop and allows you to control the starting and ending values of that variable, as well as the increment it is changed by:
process repeat for integer i from 9 to 27 by 3 output "d" % i || " " again
You can use a repeat over
loop to perform the same action on each item on a shelf in turn:
global stream some-names variable initial {"Bob", "Doug", "Andy", "Greg"} process repeat over some-names output some-names || "%n" again
You can use a repeat scan
to scan a data stream. The repeat scan
loop will continue to match data from the source until the source is exhausted or it encounters data that is not matched by any of its alternatives:
process local stream guesses initial {"sufklbp"} repeat scan "abcdefghijklmnopqrstuvwxyz" match any => the-letter do unless guesses matches any** ul the-letter output the-letter else output "*" done again