• Keine Ergebnisse gefunden

FINDING OLD TEXT AND SUBSTITUTING NEW TEXT

There will be numerous occasions when you will want to find a specific word or group of words, and move the character pointer at the same time. There will also be times when you will want to substitute a new word or group of words for an old one. For example, you might write a program that displays the name of your product (for example, "ACME Solar Controller") in several different places. Sometime later, you find out that the marketing people changed the name to "ACME Climate Controller". With one simple CREDIT command, you could substitute the new word ("Climate") for the old word ("Solar") wherever the old word occurs in the text file. You would only have to make sure that you specified the old name using upper and lower case characters as they appear in the file, and that the new name looks exactly as it should look.

Chapter 3

34

The F (Find) command finds any string (group of characters) you specify. The S (Substitute) command finds the old string you specify and substitutes the new string you specify, and rear-ranges the text so that spaces aren't introduced into the file. The SQ (Substitute after Query) command finds the old string you specify, then asks you for a yes-or-no ansWer: a yes tells CREDIT to substitute the new string you specified, and a no tells CREDIT not to substitute the new string. If you executed the SQ command iteratively (see next section), CREDIT would con-tinue looking for more instances of the old string.

To show examples of these commands, we'll return to our Pidgin Pascal text file to add some new text and substitute a new word for an old one. Use the F command to find the string

"simply continue":

*F/simply continue/<cr>

When the F command finishes, it displays the asterisk once again. To see where it put the character pointer, type CNTL-V. The cursor should be under the space after the last letter of

"continue." We want to rewrite the sentence so that "no method" is one of the methods used to operate the climate system.

At this point, it is easy to move the cursor to the appropriate place to insert new text. You can type over the old text, and use the RETURN key to continue typing a line. You can also use the CNTL-A combination to insert a lot of text. After inserting the new text, the sentences should read as follows:

If there is no demand, choose "no method" as the method,t and operate the system with t his method.t

If there is a demand for heat, det e rmi ne the heating and operate the system with t his method.t If there is a demand for coLd, determine the cooLing

and operate the system with t his method.t t

If no method is possibLe (abnormaL conditions),t shut down the cLimate system.t

method,t method,t

To illustrate use of the Sand SQ commands, we will substitute the word "request" for

"demand" throughout our text file (and thereby make our program more polite). First, use the JTT command to move the character pointer to the beginning of the file; then use the S com-mand in the following manner:

*JTT<cr>

*S/demand/request/<cr>

The S command found the first instance of "demand" and substituted "request" for it. Check to see the result by typing CNTL-V:

Based on temperature data, see if there is a request,t and determine the type of demand.t

Note that S command only found the first instance of "demand" and sUbstituted "request" for it only. Note also that the S command must have moved the comma after the first "demand" in order to fit the word" req uest" in that place.

To execute the S or SO commands repeatedly, you would use a form of command iteration.

The SO command performs the same operation as the S command if you answer with a yes; on a no answer, the SO command does not substitute text. Here is an example of the SO com-mand, with a sneak preview of the easiest form of command iteration:

*'<SQ/demand/request/><cr>

The angle brackets around the entire SO command, in conjunction with the exclamation point, cause the SO command to be executed repeatedly until the command reaches the end of the text file. Do not be confused by the angle brackets surrounding the "cr"-that is the symbol depicting use of the RETURN key. Angle brackets delimit the command to be executed iteratively, and the exclamation point replaces a number that would specify the number of iterative executions. This is explained in more detail in the next section.

The

sa

command displays the line that contains the old text, and then displays a question mark. You must respond with a "Y" for yes, or an "N" for no:

and determine the type of demand.t

?y

You do not have to type RETURN after typing the "Y" or "N", because CREDIT is expecting such an answer. The SO command goes on to find more instances of "demand":

If there is no demand, choose' 'no method" as the method,t

? Y

If there is a demand for heat, determine the heating method,t

?y

If there is a demand for co l d, determine the cooling method,t

?y

*

When the

sa

command has found the end of the text file, the condition for iterative execution is satisfied, and the execution ends. Type CNTL-V, followed by a CNTL-P, to see the previous page and the substitutions:

Maintain the climate of a bui lding using a system comprised oft heating and cooling methods.t

t

Startup the climate system.t t

Whi le the system is operating, do (and repeat) the following:t t

Get the data needed for each pass: the time, the temperatures,t the weather, the state of the solar collector, etc.t

Store this data.t

t

Based on temperature data, see if there is a request,t and determine the type of request.t

If there is no request, choose' 'no method" as the method,t and operate the system with this method.t

If there is a request for heat, determine the heating method,t and operate the system with this method.t

If there is a request for cold, determine the cooling method,t and operate the system with this method.t

t

If no method is possible {abnormal conditions),t shut down the climate system.t

t

Chapter 3

36