在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原文发表于网易博客 2010-11-16 13:39:42 learning perl第5章的练习题有点意料之外,虽然第5章讲的是基本输入输出,但是实现习题时,还是花了我一段时间用来去前面几个章节回顾了一下。呵呵,把数组知识也用进去了。 第1题: 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: }
第3题: 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);
2011-05-25 00:58
|
请发表评论