Originally published on Netease blog 13:39:42
The exercise in chapter 5th of learning Perl is somewhat unexpected. Although Chapter 5th describes basic input and output, it takes me some time to review the exercises in the previous chapters. Oh, I used the array knowledge.
Question 1st:
1: #!perl -w
2: #tac
3: use strict;
4: use 5.10.1;
5: my @result;
6: #@ARGV=qw(test1.txt test2.txt);
7: while(<>){
8: chomp($_);
9: push (@result,$_);
10: }
11: while(@result){
12: say pop (@result);
13: }
Question 3rd:
1: #!perl -w
2: sub printRuler{
3: if(@_ != 1){
4: print "should have one argument.\n";
5: return;
6: }
7:
8: my $index=1;
9: my $count=$_[0];
10: my $current=0;
11:
12: if( $count < 0){
13: $count =0;
14: }
15:
16: while($current <$count){
17: print "$index";
18: $index++;
19: if($index == 10 ){
20: print "0";
21: $index=1;
22: $current++;
23: }
24:
25:
26: }
27: print "\n";
28: }
29: sub printString{
30: &printRuler(5);
31: my $length=shift @_;
32: my $format="%${length}s\n";
33: while(@_){
34: printf $format,$_[0];
35: shift @_;
36: }
37: }
38: my @hi=qw(30 hello good-bye);
39: &printString(@hi);
I haven't read Perl recently. This language is a magic language. The same variable is put in different contexts, and the meaning of the expression is completely different. it takes time every week to gain a solid understanding and use of Perl. otherwise, it is easy to forget. er, at least it is easier to forget than C, Java, and so on.