yzwj2004 发表于 2015-12-29 08:41:21

Learning Perl 第3章习题答案

  原文发表在网易博客 2010-11-04 21:22:33
  3.1 列表打印
  #从STDIN中读取每个列表值时,都会经过chomp的处理,因此每个列表元素都被去掉了结尾的换行符   
chomp(@lines=<STDIN>);   
print &quot;print the list\n&quot;;   
foreach$line (@lines){   
    print &quot;$line\t&quot;;   
    }   
print &quot;\n&quot;;   
print &quot;print the list reverse\n&quot;;   
@lines_reverse=reverse(@lines);   
foreach $list (@lines_reverse){   
    print &quot;$list\t&quot;;   
    }
  3.2 按索引打印已有列表
  #print array2   
@namelist=qw(fred barney betty wilma dino);   
chomp(@indexlist=<STDIN>);   
print &quot;@namelist&quot;;   
foreach $index (@indexlist){   
    print &quot;the $index element in list is :\t$namelist[$index]\n&quot;;   
    }
  
  3.3 列表内容以同行和不同行方式输出
  chomp(@namelist=<STDIN>);   
@sortedNamelist=sort(@namelist);   
print &quot;print in one line:\t@{sortedNamelist}\n&quot;;   
print &quot;print elements in list in different lines;\n&quot;;   
foreach $element (@sortedNamelist){   
    print &quot;$element\n&quot;;   
    }
页: [1]
查看完整版本: Learning Perl 第3章习题答案