home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeLearning Perl, 3rd EditionSearch this book

6.6. Exercises

See Section A.5, "Answers to Chapter 6 Exercises" for answers to the following exercises:

  1. [7] Write a program that acts like cat, but reverses the order of the output lines. (Some systems have a utility like this named tac.) If you run yours as ./tac fred barney betty, the output should be all of file betty from last line to first, then barney and then fred, also from last line to first. (Be sure to use the ./ in your program's invocation if you call it tac, so that you don't get the system's utility instead!)

  2. [8] Write a program that asks the user to enter a list of strings on separate lines, printing each string in a right-justified 20-character column. To be certain that the output is in the proper columns, print a "ruler line" of digits as well. (This is simply a debugging aid.) Make sure that you're not using a 19-character column by mistake! For example, entering hello, good-bye should give output something like this:

    123456789012345678901234567890123456789012345678901234567890
                   hello
                good-bye
  3. [8] Modify the previous program to let the user choose the column width, so that entering 30, hello, good-bye (on separate lines) would put the strings at the 30th column. (Hint: see the section Section 2.6.1, "Interpolation of Scalar Variables into Strings" in Chapter 2, "Scalar Data" about controlling variable interpolation.) For extra credit, make the ruler line longer when the selected width is larger.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.