Section 8.10. Exercises


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.[]

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.

[] Dont 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 yest. If the input is I, Wilma!, the extra memory should have one character. Does your pattern still match just plain wilma?



  5. Learning Perl
    Learning Perl, 5th Edition
    ISBN: 0596520107
    EAN: 2147483647
    Year: 2003
    Pages: 232

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net