Learning Perl Chapter 6 Exercise questions

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.