Perl语言入门-第六章-哈希-习题
2012-01-16 11:16
155 查看
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
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
相关文章推荐
- 《快学Scala》第六章习题解答
- 【DX11习题学习三】第六章练习 Drawing in Direct3D(中)
- JAVA语言程序设计基础课后习题第六章
- Python核心编程 第六章课后习题
- 谭浩强第六章习题答案
- C++Primer第五版 第六章习题答案(11~20)
- How to think like a Computer Scientist: 课后习题第六章 1-5
- py第六章习题
- 《Perl语言入门》第四版习题(7)
- 《C++ Primer》第五版课后习题解答_第六章(3)(16-26)
- 《Unix/linux编程实践教程》第六章部分习题
- 第六章 Young氏矩阵 性质 习题6.3
- c++ primer(第五版)习题答案代码版(第六章)函数
- C++Primer第五版 第六章习题答案(1~10)
- C++Primer第五版 第六章习题答案(21~30)
- How to think like a Computer Scientist: 课后习题第六章 6-12
- C++Primer第五版 第六章习题答案(41~50)
- Thinking in Java(第四版)习题--第六章
- python从入门到实践 第六章习题 (高级编程技术 week3-2)
- py第六章习题