Section 5.11. Exercises

5.11. Exercises

See Appendix A 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 the last line to the first, then barney and then fred, also from the last line to the first. (Be sure to use the ./ in your program's invocation if you call it tac so 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 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 entering 30, hello, good-bye (on separate lines) would put the strings at the 30th column. (Hint: See the section in Chapter 2 about controlling variable interpolation.) For extra credit, make the ruler line longer when the selected width is larger.

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

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: