原文发表在网易博客 2010-11-04 21:22:33

3.1 列表打印

#从STDIN中读取每个列表值时,都会经过chomp的处理,因此每个列表元素都被去掉了结尾的换行符
chomp(@lines=<STDIN>);
print "print the list\n";
foreach  $line (@lines){
    print "$line\t";
    }
print "\n";
print "print the list reverse\n";
@lines_reverse=reverse(@lines);
foreach $list (@lines_reverse){
    print "$list\t";
    }

3.2 按索引打印已有列表

#print array2
@namelist=qw(fred barney betty wilma dino);
chomp(@indexlist=<STDIN>);
print "@namelist";
foreach $index (@indexlist){
    print "the $index element in list is :\t$namelist[$index]\n";
    }

 

3.3 列表内容以同行和不同行方式输出

chomp(@namelist=<STDIN>);
@sortedNamelist=sort(@namelist);
print "print in one line:\t@{sortedNamelist}\n";
print "print elements in list in different lines;\n";
foreach $element (@sortedNamelist){
    print "$element\n";
    }

相关文章:

  • 2021-05-27
  • 2022-01-13
  • 2021-05-22
  • 2021-08-19
  • 2022-02-23
  • 2021-11-13
  • 2021-07-09
  • 2021-12-15
猜你喜欢
  • 2022-02-11
  • 2021-10-12
  • 2022-02-10
  • 2022-02-05
  • 2021-05-22
  • 2022-12-23
  • 2022-02-28
相关资源
相似解决方案