您的位置:首页 > 其它

Perl为数组排序

2015-07-30 22:23 225 查看
1 #!/usr/local/bin/perl5
2 use  strict;
3 use warnings;
4 use Data::Dumper;
5 my @array=(8,99,90);
6 my $maxCnt = &max(@array);
7 sub max { my $currentMaxCnt = shift @_;
8     foreach ( @_ )
9     { if ( $_ > $currentMaxCnt )
10           {
11                 $currentMaxCnt = $_;
12                   }
13     }
14     return $currentMaxCnt;
15 }
16 print $maxCnt,"\n";
~
~
~
~
~
~
~
0mnsdev13:scripts> ls
FASTA                      FASTA.pl*                  oekb_xml_download_url.pl
FASTA.PL                   FASTa.pl                   oekb_xml_parse_load.pl
FASTA.out                  fasta.pl                   pl
mnsdev13:scripts> ./FASTA.pl
99
mnsdev13:scripts>


2.

#!/usr/local/bin/perl5
2 use strict;
3 use warnings;
4 use Smart::Comments;
5 my @array=(3,7,5,9);
6 my @arr=sort@array;
7 ###@arr;
8 print $array[3];


3.

1 #!/usr/local/bin/perl5
2 use  strict;
3 use warnings;
4 my @array = (8,99,90);
5 print join ('',sort@array),"\n";
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
0mnsdev13:scripts> ls
FASTA                      FASTA.pl*                  oekb_xml_parse_load.pl
FASTA.PL                   fasta.pl                   pl
FASTA.out                  oekb_xml_download_url.pl
mnsdev13:scripts> FASTA.pl
ksh: FASTA.pl:  not found
mnsdev13:scripts> ./FASTA.pl
89099
mnsdev13:scripts>


4.

1 #!/usr/local/bin/perl5
2 use  strict;
3 use warnings;
4 my @array = ();
5
6
7 while (1) {
8             print "input a number or <enter> to finish: ";
9                     my $a=<STDIN>;
10                             chomp ($a);
11
12                                     if ($a eq "") {
13                                                         last;
14                                                                 }
15                                             print "input $a\n";
16                                                     push(@array, $a);
17 }
18
19 print "\narray=@array\n";
20
21 my @sorted_array = sort @array;
22
23 printf "\nsorted_array = @sorted_array\n";
~
~
~
~
~
~
0mnsdev13:scripts> ls
FASTA                      FASTA.pl*                  pl
FASTA.PL                   oekb_xml_download_url.pl
FASTA.out                  oekb_xml_parse_load.pl
mnsdev13:scripts> ./FASTA.pl
input a number or <enter> to finish: 1
input 1
input a number or <enter> to finish: a
input a
input a number or <enter> to finish: b
input b
input a number or <enter> to finish: ddfg
input ddfg
input a number or <enter> to finish: ffsgs
input ffsgs
input a number or <enter> to finish: f
input f
input a number or <enter> to finish: s
input s
input a number or <enter> to finish: d
input d
input a number or <enter> to finish: d
input d
input a number or <enter> to finish: sorted_array=1 a b ddfg ffsgs f s d d
input sorted_array=1 a b ddfg ffsgs f s d d





5.REALBasic折叠

Private Sub Form_Load()

Dim a,c As Variant

Dim i As Integer,j As Integer,temp As Integer

a = Array(17,45,12,80,50)

For j = 0 To UBound(a) - 1 

For i = 0 To UBound(a) - 1 

If (a(j) > a(i)) Then 

temp = a(j) 

a(j) = a(i) 

a(i) = temp 

End If 

Next 

Next 

For i=0 to UBound(a)

msgbox str(a(i)) 

Next 

End Sub


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: