Perl语言入门-第十章-其他控制结构-习题

1. 题目

2. 代码与输出

 1  # -----------------------------------------------------------#
 2  # Source: Learning Perl, chapter10,exercise-1
 3  # Date:   2012-01-20
 4  # Author: xiaodongrush
 5  #-----------------------------------------------------------#
 6  use  5.010;
 7  $secret_num =  int( 1 +  rand  100); 
 8  while(<STDIN>) {
 9    chomp;
10    if(/^[ 0- 9]+$/) {  #  键入都是数字
11       if( $_ <  $secret_num) {
12        say  " Too low. Try again. ";
13     }  elsif( $_ >  $secret_num) {
14        say  " Too high. Try again. ";
15     }  else {
16        say  " You get it. ";
17         last;
18     }
19   }  elsif(/^quit$|^ exit$|^\s*$/) {  #  quit,exit,空白行
20       last;
21   }  else {
22     say  " Not Vaild Input. Try again ";
23   }
24 }
25  # -----------------------------------------------------------#

 1  # -----------------------------------------------------------#
 2  # Source: Learning Perl, chapter10,exercise-2
 3  # Date:   2012-01-20
 4  # Author: xiaodongrush
 5  #-----------------------------------------------------------#
 6  use  5.010;
 7  $secret_num =  int( 1 +  rand  100);
 8  if( @ARGV[ 0] =~ /-debug/i) {
 9   say  " [DEBUG] The secret num is $secret_num ";
10 }
11  while(<STDIN>) {
12    chomp;
13    if(/^[ 0- 9]+$/) {  #  键入都是数字
14       if( $_ <  $secret_num) {
15        say  " Too low. Try again. ";
16     }  elsif( $_ >  $secret_num) {
17        say  " Too high. Try again. ";
18     }  else {
19        say  " You get it. ";
20         last;
21     }
22   }  elsif(/^quit$|^ exit$|^\s*$/) {  #  quit,exit,空白行
23       last;
24   }  else {
25     say  " Not Vaild Input. Try again ";
26   }
27 }
28  # -----------------------------------------------------------#

 1  # -----------------------------------------------------------#
 2  # Source: Learning Perl, chapter10,exercise-3
 3  # Date:   2012-01-20
 4  # Author: xiaodongrush
 5  #-----------------------------------------------------------#
 6  use  5.010;
 7  $ENV{ " ZERO "} =  0;
 8  $ENV{ " EMPTY "} =  '';
 9  $ENV{ " UNDEFINED "} =  undef;
10  $max_key_len =  0;
11  $key_len_limit =  10;
12  $value_len_limit =  10;
13  while(( $key, $value) =  each  %ENV) {
14    if( length( $key) >  $key_len_limit) {
15      next;
16   }
17    $max_key_len =  $max_key_len >  length( $key) ?  $max_key_len :  length( $key);
18 }
19 say  " 1234567890 " x  7;
20  while(( $key, $value) =  each  %ENV) {
21    if( length( $key) >  $key_len_limit ||  length( $value) >  $value_len_limit) {
22      next;
23   }
24    printf  " % " .  $max_key_len .  " s     " .  " %s\n "$key$value //  " (undefined) ";  
25 }
26  # -----------------------------------------------------------#
27  # 如果$value='',那么$value || "(undefined)" 返回"(undefined)"
28  # 如果$value='',那么$value // "(undefined)" 返回'';
29  # 如果$value=undef,$value || "(undefined)" 
30  # 和$value // "(undefined)"都返回"(undefined)"
31  #-----------------------------------------------------------#

 

3. 文件

    /Files/pangxiaodong/LearningPerl/ch10-answer.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值