|
|||||
Variables | |||||
Related Syntax |
In OmniMark there are three kinds of variables, each of which can hold a different type of data. Stream variables are used to store string values, counter variables to store numeric values, and switch variables to store Boolean (true/false) values.
Variables can be either global
or local
. The difference between these is that global variables exist (and can be used) everywhere within a program, and local variables only exist (and can be used) within the rule or function where they are declared.
Since variables cannot be used until they are declared, global variable declarations usually appear at the top of an OmniMark program, and local variables appear at the beginning of the rule or function in which they are to be used. The "scope" of a variable (global or local) must be indicated in the variable declaration.
A variable declaration that creates a global counter variable named "count1" looks like this:
global counter count1
Once declared, the variable "count 1" can be used to store any positive or negative integer value.
To create a local stream variable named "quotation", you would use the variable declaration:
local stream quotation
To store a string in a stream variable, you can use the set
keyword. For example:
set quotation to "Is this a dagger I see before me?"
Counter variable values can be set and changed the same way as stream variables using the set
action, but counter variables can also be manipulated using the increment
and decrement
actions. For example, to increase the value of the count1
variable by 1, you need only say:
increment count1
It is possible to increment or decrement the value of a counter variable by the value of another counter variable. For example, you could decrement the value of "count1" by the value of "count2" with the following code:
decrement count1 by count2
The following is a program that makes use of a global switch variable to decide which output action should be executed:
global switch question process set question to true process do when question ;checks if question is true output "to be" else output "not to be" done
Note that the output of this program will always be "to be".
It is possible to declare a variable with an initial value:
global counter count2 initial {3} global stream quotation2 initial {"A horse!"} global switch status2 initial {true}
You can set a variable to the value of another variable. For example, the process rule in the following program will set the value of the global counter variable "var1" to the value of the local counter variable "var2":
global counter var1 process local counter var 2 set var2 to 8 set var1 to var2 process output "%d(var2)"
When counter and switch variables are created in OmniMark, they have default values of "1" and "false", respectively. Stream variables are somewhat different, however, because the default state of a stream is "unattached". Unless a value has been stored in a stream variable, there is no value in that variable for you to access. In database terms, the default value of a stream variable is "null".
For all intents and purposes, there is no practical limit on the number of variables that can be declared in an OmniMark program, nor is there a practical limit on the size of the numbers or strings that can be stored within them.
Related Syntax global, local |
---- |