swirl
Guide to OmniMark 9   OmniMark home
docs home 
IndexConceptsTasksSyntaxLibrariesLegacy LibrariesErrors
 
    Related Topics  

Conditional constructs

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 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.

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...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 | 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.

  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 files, streams, or the values of stream variables (as above).

Conditions based on types

To create conditional constructs based on the type of an extended record variable, use do select-type.

    Related Topics
 
 

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

OmniMark 9.1.0 Documentation Generated: September 2, 2010 at 1:35:14 pm
If you have any comments about this section of the documentation, please use this form.

Copyright © Stilo International plc, 1988-2010.