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 in Chapter 2 about controlling variable interpolation.) For extra credit, make the ruler line longer when the selected width is larger.

 

solution
#!perl -w
use strict;
use 5.010;

print "Please input string's width: ";
chomp(my $width = <>);

say
"1234567890" x ($width / 10 + 1);

printf "%".$width."s\n", "Hello World!";
printf "%${width}s\n", "Hello World!";

 

 

posted on 2010-03-22 15:37  cppfans  阅读(85)  评论(0编辑  收藏  举报