WORDPERFECT MACRO TUTORIAL


8. Variables

8.1 What are variables

Technically a variable is a particular location in the memory of the computer, but this is not a very useful way to think of it for the purpose of writing macros. A better way is to think of a variable is a container (a holder, a bag, a pot, a box, a warehouse, or however you want to think of it). The reason it is called a variable is that the contents can change. The contents, or value, of the variable can be changed as often as necessary. But a variable can have one value at a time—when another value is put into a variable, it replaces whatever was there before. A variable might also hold nothing at some point (i.e., it can be "empty").

Variables provide a way for the macro to store or "remember" data, so that the macro can use that data. You can use variables to retain or manipulate text, numbers, codes, or other information, within the macro. Variables also provide a means for comparing values, for decisions, looping, and other operations. These examples are just a small sample of what can be done with variables. Whatever you could do to or with the contents of a variable, you can do to. or with, that variable.

When you use a variable, you are really just using it as a substitute for the value the variable contains. The advantage of using a variable is the flexibility it provides. If you "hard-code" fixed or constant values in your macro, the macro will do exactly the same thing each time it runs. You can make that macro much more flexible if you can use variables, with which the macro can do different things each time it runs, depending on the value that the variables have when the macro runs.

8.2 Naming variables

Each variable has a name, which is used to identify the variable. You (as the macro programmer) choose the name of each variable you create. There are rules regarding the naming of variables, but it is easiest just to follow this one: the variable name must start with a letter, and the rest of the name can consist of a combination of letters and numbers; spaces are not allowed. As with commands, variable names are not case-sensitive. Variable names can be as simple as one letter, like the classic variable name x, but as you write more complex macros it is easier to use variable names that are more descriptive. A variable that will be used to keep track of repeated actions might be called Count; a variable that will contain a person's name might be called PersonName, or NameofPerson.

One limitation on naming variables is that certain words are used in PerfectScript (WordPerfect's macro language) for programming commands; these words are reserved for use as PerfectScript commands only, and cannot be used to name variables. To avoid accidentally using a reserved word as a variable name, and to readily identify it as a variable, some programmers start each variable name with a small "v" (e.g., vCount, vPersonName).

Remember that when we want to refer to literal characters (a string), we enclose them in quotes. Example: Type("xyz"). When we use a variable, however, we are not referring literally to the name of the variable, but to the contents of the variable. Therefore, variable names are not enclosed in quotes. This is how the macro distinguishes between a string (in quotes) and a variable name (not in quotes).

8.3 Creating variables

Variables are created during the process of writing/editing (not recording) a macro. You create variables as you need them. A variable is created by the action of giving a value to a variable (called assigning a value to the variable). The first time you put something into a variable, the variable comes into existence automatically. Unlike some other programming languages, in PerfectScript you do not have to declare or define a variable before you can assign a value to the variable or otherwise use it.

A special kind of variable is created and maintained by the WordPerfect program itself, as opposed to the macro writer. These special variables are known as system variables, which contain information about the current state of the Wordperfect program. System variables are discussed in the next section of this tutorial.

8.4 Assigning values to variables

The process of putting a value into a variable is called "assignment." The first time you assign a value to a variable, you automatically create the variable. There are three different forms of assigning a value to a variable; they can be used interchangeably. These forms are demonstrated by the following three assignment statements, each of which assigns the string "foo" to the variable x:

x:="foo"
(this is the easiest method#8212note the colon equal sign)
x="foo"
(the bare equal sign can be confusing, since it is the same operator as that used in conditions)
Assign(x;"foo")
(uses the explicit PerfectScript command Assign() which takes two parameters: the first is the name of the variable, the second is the value assigned to the variable)

Once an assignment is performed, we can say that the variable "contains" what was assigned, or "has the value" of what was assigned. In the examples above, we can say that x now contains the string "foo" (whatever was in the variable before the assignment is gone). If variable x did not exist before this assignment, this first assignment creates it.

The variable name really represents the contents of the variable, so you can assign the value of one variable to another. Remember that the operation is from right to left, i.e., the value on the right is assigned to the variable on the left. If x contains the string "foo" from the assignment above, we can assign "foo" to variable y by assigning the value of x to y.

y:=x

This assigns whatever is in variable x when the assignment is made, to variable y. You can think of this as making a copy of whatever was in variable x, and putting that copy into variable y. Since you've made a copy, changing the original doesn't affect the copy. x and y are independent variables, and changing the value of x after the assignment has no effect on the value of y.

Remember the difference between strings and variable names. Strings are enclosed in quotes; variable names are not. So the assignment y:="xyz" assigns the string "xyz" to y. The assignment y:=xyz assigns a copy of the contents of variable xyz to y.




© 1999 Seth H. Katz
All rights reserved


E-MAIL TO AUTHOR


BACK Table of
Contents NEXT