root@darkstar:/home/tiger506/Program/Perl# cat -n ./p_uniq_num.pl
1 #!/usr/bin/perl
2 use strict;
3 my @num_array=(1,2,5,4,2,3,1,6,5);
4 my $num_count=$#num_array+1;
5 my $tmp=undef;
6
7
8 for(my $i=0;$i<$num_count;$i++){ #将数组按照由大到小的顺序排列
9 for(my $j=$i+1;$j<$num_count;$j++){
10 if($num_array[$i]<$num_array[$j]){
11 $tmp=$num_array[$i];
12 $num_array[$i]=$num_array[$j];
13 $num_array[$j]=$tmp;
14 }
15 if($num_array[$i]==$num_array[$j]){
16 delete @num_array[$j]; #剔除重复的元素
17 }
18 }
19
20 }
21
22 print "剔除数组中冗余元素后,将数组由大到小排序: ";
23 my @new_array;
24 foreach my $element(@num_array){
25 print $element."_";
26 if($element=~m/\d/){
27 push @new_array,$element;
28 }
29 }
30 print "\n";
31 print"数组长度为".($#new_array+1)."\n";
32
结果:
root@darkstar:/home/tiger506/Program/Perl# ./p_uniq_num.pl
剔除数组中冗余元素后,将数组由大到小排序: 6_5_4_3_2_1_
数组长度为6
1 #!/usr/bin/perl
2 use strict;
3 my @num_array=(1,2,5,4,2,3,1,6,5);
4 my $num_count=$#num_array+1;
5 my $tmp=undef;
6
7
8 for(my $i=0;$i<$num_count;$i++){ #将数组按照由大到小的顺序排列
9 for(my $j=$i+1;$j<$num_count;$j++){
10 if($num_array[$i]<$num_array[$j]){
11 $tmp=$num_array[$i];
12 $num_array[$i]=$num_array[$j];
13 $num_array[$j]=$tmp;
14 }
15 if($num_array[$i]==$num_array[$j]){
16 delete @num_array[$j]; #剔除重复的元素
17 }
18 }
19
20 }
21
22 print "剔除数组中冗余元素后,将数组由大到小排序: ";
23 my @new_array;
24 foreach my $element(@num_array){
25 print $element."_";
26 if($element=~m/\d/){
27 push @new_array,$element;
28 }
29 }
30 print "\n";
31 print"数组长度为".($#new_array+1)."\n";
32
结果:
root@darkstar:/home/tiger506/Program/Perl# ./p_uniq_num.pl
剔除数组中冗余元素后,将数组由大到小排序: 6_5_4_3_2_1_
数组长度为6
本文转自 tiger506 51CTO博客,原文链接:http://blog.51cto.com/tiger506/1342766,如需转载请自行联系原作者