swirl Guide to OmniMark 5   OmniMark home
docs home 
IndexConceptsTasksSyntaxLibrariesOMX VariablesErrors
 
    Related Syntax  
Conditional constructs

When you want a program to do one of several possible things in different situations, use a conditional construct. OmniMark provides three different forms of conditional construct, each 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 count1 = 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 count1 is equal to 4.

The simplest of the conditional constructs is the "do when...done" block. This allows you to have an OmniMark program perform various actions based on the results of one or more tests.

  do when count1 = 4
     output "Yes, the value of count1 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

You can have a "do when" block perform a set of actions if a variable is of more than one value, by adding those conditions to the header using the or keyword. For example:

  do when count1 = 1 or count1 = 5
     output "count1 is one or five%n"
  else
     output "the value of count1 is not one or five%n"
  done

"Do when" blocks can be much more complex than this, however, since "else when" phrases are also allowed.

  do when count1 = 4
     output "Yes, the value of count1 is four%n"
  else when count1 = 5
     output "The value of count1 is five%n"
  else when count1 = 6
     output "The value of count1 is six%n"
  else
     output "The value of count1 is not 4, 5, or 6%n"
  done

Another form of conditional construct is the "do select...done" construct:

  do select count1
     case 1 to 5
        output "count1 is within the first range%n"
     case 6 to 10
        output "count1 is within the second range%n"
  done

The program won't do anything if the value of count1 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" phrase to the construct:

  do select count1
     case 1 to 5
        output "count1 is within the first range%n"
     case 6 to 10
        output "count1 is within the second range%n"
     else
        output "count1 is out of range%n"
  done

Note that while "else" phrases can be used within a "do select" construct, "else when" phrases cannot.

If you want the program to do something when a variable is equal to a particular value, you have to specify that within another "case" phrase. For example:

  do select count1
     case 1 to 4
        output "count1 is in the first range%n"
     case 5
        output "count1 is equal to 5%n"
     case 6 to 10
        output "count1 is in the second range%n"
     else
        output "count1 is out of range%n"
  done

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.

  global stream day

  process
     set day to date "=W"
     do scan day
        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 files, streams, or the values of stream variables (as above).

      Related Syntax
   do when, do unless
   when, unless
 
----

Top [ INDEX ] [ CONCEPTS ] [ TASKS ] [ SYNTAX ] [ LIBRARIES ] [ OMX ] [ OMX ] [ ERRORS ]

Generated: August 11, 2000 at 3:06:17 pm
If you have any comments about this section of the documentation, send email to docerrors@omnimark.com

Copyright © OmniMark Technologies Corporation, 1988-2000.