Документация
HTML CSS PHP PERL другое

Section 8.10.  Exercises

 
Previous
Table of Contents
Next

8.10. Exercises

See Appendix A for answers to the following exercises.

Several of these exercises ask you to use the test program from this chapter. You could manually type this program, taking great care to get all of the odd punctuation marks correct.[*] But you'll probably find it faster and easier to download the program and some other goodies from the O'Reilly web-site, as we mentioned in the Preface. You'll find this program under the name pattern_test.[Section 8.10.  Exercises]

[*] If you do type it up on your own, remember that the backtick character (`) is not the same as the apostrophe ('). On most full-sized computer keyboards these days (in the U.S., at least), the backtick is found on a key immediately to the left of the 1 key.

[Section 8.10.  Exercises] Don't be surprised if the program you download is a slightly different version than what we have in the book.

  1. [8] Using the pattern test program, make a pattern to match the string match. try the program with the input string beforematchafter. Does the output show the three parts of the match in the right order?

  2. [7] Using the pattern test program, make a pattern that matches if any word (in the \w sense of word) ends with the letter a. Does it match wilma but not barney? Does it match Mrs. Wilma Flintstone? What about wilma&fred? try it on the sample text file from the previous chapter's exercises (and add these test strings if they weren't already in there).

  3. [5] Modify the program from the previous exercise so the word ending with the letter a is captured into memory $1. Update the code to display that variable's contents in single quotes, something like $1 contains 'Wilma'.

  4. [5] Extra credit exercise: Modify the program from the previous exercise so that immediately following the word ending in a it will capture up to five characters (if there are that many characters) in a separate memory variable. Update the code to display both memory variables. For example, if the input string says I saw Wilma yesterday, the up to five characters are Section 8.10.  Exercisesyest. If the input is I, Wilma!, the extra memory should have one character. Does your pattern still match just plain wilma?

  5. [5] Write a new program (not the test program) that prints out any input line ending with whitespace (other than a newline). Put a marker character at the end of the output line so as to make the whitespace visible.

    Previous
    Table of Contents
    Next
    © 2000- NIV