一
1 #! /usr/bin/perl -w
2 @chiefs = qw(clinton bush reagan carter ford nixon);
3 print join(' ',sort @chiefs);
二
1 #! /usr/bin/perl -w
2 @arry = qw(1 5 7 2 9 3);
3
4 @arry = sort{ $a<=>$b;}@arry;
5
6 foreach $x(@arry)
7 {
8 print "$x\n";
9 }
三
1 #! /usr/bin/perl -w
2 @number = qw(1 5 9 6 2 10);
3 @number = sort{
4 return(1) if($a > $b);
5 return (0) if($a == $b);
6 return(-1) if($a < $b);
7 }@number;
8
9 #foreach $x(@sorted)
10 # print"$x\n";
11
12 #@number = sort{$a<=>$b;}@number;
13
14 foreach $y(@number)
15 {
16 print "$y\n";
17 }
初学,不知道为什么 @sorted = sort{...}@number;这样会是错误的。知道的请留言,谢谢!
阅读(1776) | 评论(2) | 转发(0) |