liuhongyu 发表于 2015-12-28 08:35:23

learning perl第5章练习题

  原文发表于网易博客 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 &quot;should have one argument.\n&quot;;
   5:    return;
   6:    }
   7:   
   8:   my $index=1;
   9:   my $count=$_;
10:   my $current=0;
11:   
12:   if( $count < 0){
13:    $count =0;
14:    }
15:   
16:   while($current <$count){
17:    print &quot;$index&quot;;
18:    $index++;
19:    if($index == 10 ){
20:   print &quot;0&quot;;
21:   $index=1;
22:   $current++;
23:   }
24:   
25:   
26:    }
27:   print &quot;\n&quot;;
28:   }
29:sub printString{
30:   &printRuler(5);
31:   my $length=shift @_;
32:   my $format=&quot;%${length}s\n&quot;;
33:   while(@_){
34:    printf $format,$_;
35:    shift @_;
36:    }
37:   }
38:   my @hi=qw(30 hello good-bye);
39:   &printString(@hi);
  2011-05-25 00:58
最近没在看perl了,这个语言是个神奇的语言,同样一个变量,放在不同的语境中,表达的意思就完全不一样.必须每周都花点时间才能稳固对perl的理解和使用.否则容易忘掉.呃,至少比c,java之类的容易忘掉.
页: [1]
查看完整版本: learning perl第5章练习题