|
That's a mighty big "IF"
The IF statement is one of the most basic concepts in computer and macro programming. If you don't understand the IF statement you won't be able to program effectively, whether it be macros or even stand-alone applications. The IF statement is an essential element in advanced macro programming.
Analyzing the IF
What's an IF statement? It's actually quite simple, we use them every day. I used one in the previous paragraph, "If you don't understand the IF statement you won't be able to program effectively..." This IF statement creates a condition and tells the result. Thus IF statements are also called conditional statements. If the condition in the IF statement is true then the result will happen. If you can't understand IF statements you can't program effectively. The condition is whether or not you understand the IF statement. If you do you can program, if you don't you can't program well.
Your computer will evaluate the IF statement and if that statement is true the computer will take a specified course of action. IF statements let your computer decide what to do in a given set of circumstances. Let's see the IF statement in action.
A WP51 counting macro using the IF statement
Look at the following WP51 macro:
- {Home}{Home}{Home}{Down}{Enter}@
- {Home}{Home}{Home}{Up}
- {ASSIGN}VAR1~0~
- {LABEL}LOOP~
- {IF}"{SYSTEM}RIGHT~"="{Enter}"~
- {Down}
- {ELSE}
- {ASSIGN}VAR1~{VARIABLE}VAR1~+1~
- {Down}
- {END IF}
- {IF}"{SYSTEM}RIGHT~"="@"~
- {Del}{Backspace}{Home}{Home}{Home}{Up}
- {GO}PROMPT~
- {ELSE}
- {GO}LOOP~
- {END IF}
- {LABEL}PROMPT~
- {PROMPT}There.are.{VARIABLE}VAR1~.LINES.in.the.document~
- {WAIT}30~
- {Home}{Home}{Down}
Lets take this macro apart one line at a time and see how it works.
Line #1 takes us all the way to the bottom of the document just as a transcriptionist would by using the pressing the Home key 3 times followed by the Down arrow. The macro then inserts a hard return and types the character "@".
Line #2 takes us all the way to very beginning of the document as if we had pressed the Home key 3 times followed by the Up arrow.
Line #3 creates a variable and assigns it a value of 0. Don't worry about variables. Just think of them as words that can hold different and changing types of letter and number information.
Line #4 is a Label. The name of the Label is LOOP. Labels create a sort of starting point for the macro. You'll see what I mean when we get to the Go statement.
Line #5 is our beloved IF statement. This statement is telling WP to analyze the character to the right of the insertion point. The computer is then told that IF this character is equal to a Hard Return or an Enter execute the next set of commands, line 6.
Line #6 is our next set of commands, in this case Down. This is the same as if you had pushed the Down arrow key. (This is a gross line counter so if the first character on the line is a hard return or enter we assume the line is blank and move down to the next line to check it.) When the computer is done executing this line it skips lines 7-10 and starts executing the commands at line 11.
Line #7 is the other important aspect of the IF statement the Else. If the statement in line #5 was false (if the character to the right of the insertion point was not equal to a hard return or an enter) then the computer will execute the commands after the Else but before the End If statement, lines 8 & 9.
Line #8 is where the action happens. Here we are assigning the value of the variable (which we created in line #3) the value of itself + 1. So we are adding to the variable. VAR1 is where the current amount of lines in the document are stored.
Line #9 is the Down arrow.
Line #10 is the End If statement. This tells the computer that the If statement is over.
Line #11 is another If statement but this one checks to see if the character to the right of the insertion point is the @. We needed to do this and the statements in line #1 because if we didn't the macro would loop endlessly. Again, if the condition is true and the character is the @ then the next set of commands will be executed, lines 12 & 13.
Line #12 is the same as if you pressed the Delete key, the Backspace key, the Home key three times, and the Up arrow. Or in layman's terms delete the @ and go to the top of the document.
Line #13 tells the computer to find the label named PROMPT and execute the commands following it, i.e. get out of this If stuff and GO to line #17.
Line #14 is the Else. Again, if the condition is not true (the character is not equal to @) then the next set of commands will execute, those found on line #15.
Line #15 is the Go command again. This will have the computer find the label named loop and execute the commands after it. (Go back to line #4 and start all over again.)
Line #16 will get us out of this set of If commands.
Line #17 is the label named PROMPT.
Line #18 displays text on the screen that says "There are XX LINES in the document" where XX is the number of gross lines in the document.
Line #19 uses the Wait command to pause the macro for the specified number of tenths of a second. So the text in line #18 will stay on the screen for 3 seconds.
Line #20 takes us to the bottom of the document just as if we had pressed the Home key twice and the Down arrow once.
What about a character counter?
A macro that does a character count is even easier to write. Start with the gross line count macro above and remove lines 5, 7, 9, and 10. Then change line 6 to read {Right} instead of {Down}. You will also need to add a line between line 17 and 18 that says {ASSIGN}VAR1~{VARIABLE}VAR1~\65~. Change 65 to the number of characters you want per line. The completed macro should look like this:
- {Home}{Home}{Home}{Down}{Enter}@
- {Home}{Home}{Home}{Up}
- {ASSIGN}VAR1~0~
- {LABEL}LOOP~
- {Right}
- {ASSIGN}VAR1~{VARIABLE}VAR1~+1~
- {IF}"{SYSTEM}RIGHT~"="@"~
- {Del}{Backspace}{Home}{Home}{Home}{Up}
- {GO}PROMPT~
- {ELSE}
- {GO}LOOP~
- {END IF}
- {LABEL}PROMPT~
- {ASSIGN}VAR1~{VARIABLE}VAR1~/65~
- {PROMPT}There.are.{VARIABLE}VAR1~.LINES.in.the.document~
- {WAIT}30~
- {Home}{Home}{Down}
You will notice that we removed the lines that check to see if the current line is a blank line (starting with a hard return or enter). This macro counts one character for every move to the right, so it will count all codes, spaces, and formatting commands as one character each. It works exactly as though you had gone to the top of your document by hitting the Home key 3 times and then the Up arrow, opened Reveal Codes, then repeatedly hit the Right arrow key and kept a count of the number of times you pressed the Right arrow between the top of the document and the end of it. Aren't you glad we can have computers perform these menial tasks for us?
Conclusions
That's all there is to a basic line count macro in WP51. If you wanted to program these yourself you just have to pay special attention to the bolded commands. These have to be inserted. If the command is represented by a keystroke (backspace, delete, home, up, down, enter, etc.) you can have WP perform the keystroke by pressing Ctrl-V and then the key you want to execute. If the command was a macro programming command (label, go, if, else, end if, system, prompt, variable, assign, etc.) make sure that you press Ctrl-PgUp and scroll through the pop-up list to highlight the desired command. Pressing Enter will insert the highlighted command into your macro. Press F7 (Exit) when done.
Note: In both the macros above the line numbers were added for readability. You should never use line numbers in a Word Perfect macro.
Copyright 1999 by Mike DeTuri (reprinted from Computer Solutions Vol. 1, Issue 2)
|