課后練習(xí)
第一題
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!)
這里先隨便創(chuàng)建3個文本文件,分別命名為:fred,barney,betty,里面的內(nèi)容分別是:
$ 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.
題目要求分別按反向的順序輸出文本內(nèi)容秕磷,并且每一個文本里的內(nèi)容也要從最后一行輸出但荤,腳本如下:
#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
print reverse <>;
運行結(jié)果:
$ ./practice1.pl fred barney betty
Nice to meet you.
I am a dancer.
Hello ! My name is Betty. #這三行是betty文本的內(nèi)容
I am a girl.
I am a doctor.
Hello ! My name is Barney.#這三行是barney文本內(nèi)容
I am a boy.
I am a student.
Hello ! My name is Fred.#這三行是fred文本內(nèi)容
第二題
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=<STDIN>); #這里必須是用數(shù)組@去件,如果用$來定義腰素,輸入一行字符回車后匙监,就自動彈出結(jié)果了琐馆,而不是輸入多行的形式
print (("1234567890" x 7)."\n");
foreach (@line) {
printf "%20s\n",$_;
}
運行結(jié)果:
$ ./practice1.pl
Please enter some strings, then press ctrl_D:
hello #輸入第一行龟再,回車
good-bye #輸入第二行,回車审胸,ctrl+D亥宿,則彈出下面的結(jié)果
1234567890123456789012345678901234567890123456789012345678901234567890
hello
good-bye #彈出的結(jié)果一共占20個字符的位置,并進行右對齊砂沛,上面的數(shù)字是標尺
第三題
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 = <STDIN>);
print "Please enter some strings, then press ctrl_D:\n";
chomp(my @line=<STDIN>);
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
}
運行結(jié)果:
$ ./practice1.pl
Choose the column width:
30
Please enter some strings, then press ctrl_D:
hello
good-bye
1234567890123456789012345678901234567890 #標尺按照輸入的字符串長度調(diào)整烫扼,這里你會發(fā)現(xiàn)這個標尺沒有上面一題的長
hello
good-bye