原文发表在网易博客 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"; }
|
请发表评论