Learning Perl学习笔记(4)第五章 Input and Output

课后练习

第一题

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!)

这里先随便创建3个文本文件,分别命名为:fred,barney,betty,里面的内容分别是:

$ cat fred
Hello ! My name is Fred.
I am a student.
I am a boy.

$ cat barney
Hello ! My name is Barney.
I am a doctor.
I am a girl.

$ cat betty
Hello ! My name is Betty.
I am a dancer.
Nice to meet you.

题目要求分别按反向的顺序输出文本内容,并且每一个文本里的内容也要从最后一行输出,脚本如下:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;

print reverse <>;

运行结果:

$ ./practice1.pl fred barney betty
Nice to meet you.
I am a dancer.
Hello ! My name is Betty. #这三行是betty文本的内容
I am a girl.
I am a doctor.
Hello ! My name is Barney.#这三行是barney文本内容
I am a boy.
I am a student.
Hello ! My name is Fred.#这三行是fred文本内容

第二题

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:


脚本:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;

print "Please enter some strings, then press ctrl_D:\n";

chomp(my @line=); #这里必须是用数组@,如果用$来定义,输入一行字符回车后,就自动弹出结果了,而不是输入多行的形式
print (("1234567890" x 7)."\n");
foreach (@line) {
        printf "%20s\n",$_; 
}

运行结果:

$ ./practice1.pl
Please enter some strings, then press ctrl_D:
hello #输入第一行,回车
good-bye #输入第二行,回车,ctrl+D,则弹出下面的结果
1234567890123456789012345678901234567890123456789012345678901234567890
               hello
            good-bye #弹出的结果一共占20个字符的位置,并进行右对齐,上面的数字是标尺

第三题

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 “Interpolation of Scalar Variables into Strings” on page 32,
about controlling variable interpolation.) For extra credit, make the ruler line
longer when the selected width is larger.

脚本:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;

print "Choose the column width:\n";
chomp (my $width = );

print "Please enter some strings, then press ctrl_D:\n";
chomp(my @line=);

print (("1234567890" x (($width+10)/10))."\n");
foreach (@line) {
        printf "%*s\n",$width,$_; #You can specify the width as one of the arguments. A * inside the format string takes the next argument as a width
}

运行结果:

$ ./practice1.pl
Choose the column width:
30
Please enter some strings, then press ctrl_D:
hello
good-bye
1234567890123456789012345678901234567890 #标尺按照输入的字符串长度调整,这里你会发现这个标尺没有上面一题的长
                         hello
                      good-bye

你可能感兴趣的:(Learning Perl学习笔记(4)第五章 Input and Output)