• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Perl语言入门-第六章-哈希-习题

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1. 题目

2. 代码与输出

    ch6-family-name.pl

 1 #-----------------------------------------------------------#
 2 # Source: Learning Perl, chapter6, exercise-1
 3 # Date:   2012-01-16
 4 # Author: xiaodongrush
 5 #-----------------------------------------------------------#
 6 use 5.010;
 7 %family_name_hash = (
 8   "fred"   =>  "flintstone",
 9   "barney" =>  "rubble",
10   "wilma"  =>  "flintstone", );
11 
12 while(<>) {
13   chomp;
14   if(exists $family_name_hash{$_} ) {
15     say $_ . "'s family name is : " . $family_name_hash{$_};
16   } else {
17     say $_ . "'s family name not exist in hash";
18   }
19 }
20 #-----------------------------------------------------------#

   

ch6-wordcount.pl

 1 #-----------------------------------------------------------#
 2 # Source: Learning Perl, chapter6, exercise-2
 3 # Date:   2012-01-16
 4 # Author: xiaodongrush
 5 #-----------------------------------------------------------#
 6 use 5.010;
 7 while(<>) {
 8   chomp;
 9   if(exists $word_hash{$_}) {
10     $word_hash{$_} += 1;
11   } else {
12     $word_hash{$_} = 1;
13   }
14 }
15 foreach(sort (keys %word_hash) ) {
16   say $_ . "\t\t" . $word_hash{$_};
17 }
18 #-----------------------------------------------------------#

ch6-ENV-hash.pl

 1 #-----------------------------------------------------------#
 2 # Source: Learning Perl, chapter6, exercise-3
 3 # Date:   2012-01-16
 4 # Author: xiaodongrush
 5 #-----------------------------------------------------------#
 6 use 5.010;
 7 
 8 $max_key_len = 0;
 9 $max_value_len = 0;
10 $len_limit = 35;
11 while(($key$value) = each %ENV) {
12   if(length($key) < $len_limit  && length($value) < $len_limit ) {
13     $max_key_len = $max_key_len > length($key) ? $max_key_len : length($key);
14     $max_value_len = $max_value_len > length($value) ? $max_value_len : length($value);
15   }
16 }
17 $format = "%-" . $max_key_len . "s  ,  %-" . $max_value_len . "s\n";
18 printf $format"key""value";
19 foreach(sort(keys %ENV)) {
20   if( (length($_) < $len_limit ) && (length($ENV{$_}) < $len_limit )) {
21     printf $format$_$ENV{$_};
22   }
23 }
24 <STDIN>;
25 #-----------------------------------------------------------#

3. 文件

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
perl读取大文件outofmemory发布时间:2022-07-22
下一篇:
XCTF-WEB—i-got-id-200:perl网页文件+ARGV上传造成任意文件读取发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap