且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

learning perl第5章练习题

更新时间:2022-09-17 19:26:33

原文发表于网易博客 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 
最近没在看perl了,这个语言是个神奇的语言,同样一个变量,放在不同的语境中,表达的意思就完全不一样.必须每周都花点时间才能稳固对perl的理解和使用.否则容易忘掉.呃,至少比c,java之类的容易忘掉.



本文转自leipei博客园博客,原文链接:http://www.cnblogs.com/leipei2352/archive/2011/05/25/2056175.html,如需转载请自行联系原作者