When you want a program to do one of several possible things in different situations, use a conditional
construct. OmniMark provides a conditional operator as well as three different forms of conditional construct
based upon the basic do
… done
block.
It is important to note that almost anything in OmniMark can be made conditional simply by adding a
when
keyword followed by a test. For example, any rule can have conditions added to it:
find "cat" when i = 4 output "I found a cat%n"
This rule would only output I found a cat
if cat
is found in the input data and the value
of i
is equal to 4
.
You can select between two possible values using the conditional operator:
process local integer planet initial { 3 } output "Hello " || (planet = 3 -> "Human" | "Alien")
The most general of the conditional constructs is the do when
block. This allows you to have an OmniMark
program perform various actions based on the results of one or more tests. For example,
do when i = 4 output "Yes, the value of i is four%n" done
If you want the program to do one thing when a certain condition is true
and another if it is false
, you can add an else
option.
do when words matches uc output "lg" % words || "%n" else output words || "%n" done
More complex conditions can be specified in a do when
block by using the |
(or
) and
&
(and
) operators to build up larger boolean conditions. For example:
do when i = 1 | i = 5 output "Yes, the value of i is one or five%n" else output "Oh well, the value of i is not one or five%n" done
do when
blocks can be much more complex than this, however, since else when
clauses are also allowed.
do when i = 4 output "Yes, the value of i is four%n" else when i = 5 output "The value of i is five%n" else when i = 6 output "The value of i is six%n" else output "The value of i is not 4, 5, or 6%n" done
Another form of conditional construct is the do select
construct:
do select i case 1 to 5 output "The value of i is within the first range%n" case 6 to 10 output "The value of i is within the second range%n" done
The program won't do anything if the value of i
is less than 1
or greater than 10
, however, because there is no alternative that will be executed in these situations. This is quite easily
rectified, by adding an else
clause to the construct:
do select i case 1 to 5 output "The value of i is within the first range%n" case 6 to 10 output "The value of i is within the second range%n" else output "The value of i is out of range%n" done
While else
clauses can be used within a do select
construct, else when
clauses cannot.
If you want the program to do something when a expression is equal to a particular value, you have to specify that
within another case
clause. For example:
do select i case 1 to 4 output "The value of i is in the first range%n" case 5 output "The value of i is equal to 5%n" case 6 to 10 output "The value of i is in the second range%n" else output "The value of i is out of range%n" done
Conditionals constructs can also be based on the run-time type of a extended record; see do select-type
for details.
The final form of conditional constructs is a do scan
. do scan
constructs are used to examine a
piece of input data for certain patterns. If one of the patterns is discovered in the input data, a set of
specified actions is performed. For example, the following program retrieves the name of the current day and scans
it. Depending on which pattern is found, the program will output one of several possible phrases.
process do scan date "=W" match "Monday" output "I don't like Mondays.%n" match "Friday" output "I love Fridays!!!%n" else output "At least it's not Monday.%n" done
do scan
constructs can be used to scan input data in the form of file
s, string
s, or
string source
s.