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

Perl语言入门-第三章-列表与数组-习题

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

1. 题目

   

2. 代码   

 1 #-----------------------------------------------------------#
 2 # Source: Learning Perl, chapter3
 3 # Date:   2012-01-13
 4 # Author: xiaodongrush
 5 #-----------------------------------------------------------#
 6 #                          code-1                           #
 7 #-----------------------------------------------------------#
 8 $split = "-------------------------------------------------";
 9 $code1 = 'chomp(@strs = <STDIN>);'     . "\n" . 
10          'foreach(reverse(@strs)) {'   . "\n" .
11          '  print $_ . "\n";'               . "\n" .
12          '}';
13 print "code-1\n" . $code1 . "\n" . $split;
14 #-----------------------------------------------------------#
15 #                          execute-1                        #
16 #-----------------------------------------------------------#
17 print "\nexecute-1\n";
18 chomp(@strs = <STDIN>);
19 foreach(reverse(@strs)) {
20   print $_ . "\n";
21 }
22 #-----------------------------------------------------------#
23 #                          code-2                           #
24 #-----------------------------------------------------------#
25 $code2 = '@names = qw { fred betty barney dino wilma pebbles bamm-bamm };'     . "\n" . 
26          'chomp(@indexes=<STDIN>);'                                            . "\n" .
27          'foreach(@indexes) {'                                                 . "\n" .
28          '  print @names[($_-1)%7] . "\n";'                                    . "\n" .
29          '}';
30 print $split . "\ncode-2\n" . $code2 . "\n" . $split;
31 #-----------------------------------------------------------#
32 #                          execute-2                        #
33 #-----------------------------------------------------------#
34 print "\nexecute-2\n";
35 @names = qw { fred betty barney dino wilma pebbles bamm-bamm };
36 chomp(@indexes=<STDIN>);
37 foreach(@indexes) {
38   print @names[($_-1)%7] . "\n";
39 }
40 #-----------------------------------------------------------#
41 #                          code-3                           #
42 #-----------------------------------------------------------#
43 $code3 = 'chomp(@strs=<STDIN>);'      . "\n" . 
44          'foreach(sort(@strs)) {'     . "\n" .
45          '  print $_ . " ";'          . "\n" .
46          '}'                          . "\n" .
47          'print "\n";'                . "\n" .
48          'foreach(sort(@strs)) {'     . "\n" .
49          '  print $_ . "\n";'         . "\n" .
50          '}';
51 print $split . "\ncode-3\n" . $code3 . "\n" . $split;
52 #-----------------------------------------------------------#
53 #                          execute-3                        #
54 #-----------------------------------------------------------#
55 print "\nexecute-3\n";
56 chomp(@strs=<STDIN>);
57 foreach(sort(@strs)) {
58   print $_ . " ";
59 }
60 print "\n";
61 foreach(sort(@strs)) {
62   print $_ . "\n";
63 }
64 #-----------------------------------------------------------#
65 print "END!!!";
66 <STDIN>
67 #-----------------------------------------------------------#
68 # 总结
69 # 1. @str_array = <STDIN>; 每次读入一行,数组增加一个元素,
70 #    直到读入文件为空或者输入Control+Z(Windows),Control+D(Unix)
71 # 2. $_表示一些省略参数的变量,比如foreach()省略,print的省略。
72 # 3. reverse,sort,这些函数与chomp不同,更像返回值,而chomp直接修改了原数组,或者原变量

3. 输出

   

   

   

4. 文件

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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
perl抓取文件内容发布时间:2022-07-22
下一篇:
Perl&amp;amp;Python编写CGI发布时间: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