Perl 數組應用詳解(push, pop, shift, unshift)

來源:互聯網
上載者:User

標籤:長度   字串   tin   常用   詳解   取出   var   display   perl   

Perl的數組操作有四大常用函數:

push:從數組的末尾加入元素。
pop :從數組的末尾取出元素

shift: 從數組的開頭取出元素
unshift:從數組的開頭加入元素

 

1、push

#!/usr/bin/perl
use strict;
use warnings;

my @array = ();

for ( my $i = 1 ; $i <= 5 ; ++$i ) {
    push @array, $i;
    print "@array\n";
}

output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5


2、pop

#!/usr/bin/perl
####<pop>###

use strict;
use warnings;

my @array = ( 1, 2, 3, 4, 5, 6 );

while (@array) {
    my $firstTotal = pop(@array);
    print "@array\n";
}

output:

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

 

 

3、shift

#!/usr/bin/perl
####<shift>###

use strict;
use warnings;

my @array = ( 1, 2, 3, 4, 5, 6 );

while (@array) {
    my $firstTotal = shift(@array);
    print "@array\n";
}

output:

2 3 4 5 6
3 4 5 6
4 5 6
5 6
6

 

 

4、unshift

#!/usr/bin/perl
####<unshift>###

use strict;
use warnings;

my @array = ();

for ( my $i = 1; $i <= 5; ++$i ) {
   unshift( @array, $i );         # add $i to front of @array
   print "@array\n";              # display current @array
}

output:

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

 

 

另外,perl的數組還有其它重要函數,如splice、subtr、split、join、sort等:

5、splice 運算元組中間部分的函數:

5.1、向數組中間插入內容

#!/usr/bin/perl

use strict;
use warnings;

my @array = ( 0 .. 6 );
my @array1 = ( ‘a‘ .. ‘d‘ );

my @replaced = splice( @array, 3, 2, @array1 );

print "replaced:     @replaced\n",
      "with:         @array1\n",
      "resulting in: @array\n\n";

output:

replaced:     3 4
with:         a b c d
resulting in: 0 1 2 a b c d 5 6


5.2、刪除數組元素

#!/usr/bin/perl

use strict;
use warnings;

my @array  = ( 0 .. 6 );
my @array1 = ( ‘a‘ .. ‘d‘ );

my @replaced = splice( @array, 3, 2 );

print "replaced:     @replaced\n",
      "resulting in: @array\n\n";

output:

replaced:     3 4
with:         a b c d
resulting in: 0 1 2 5 6


刪除到末尾

#!/usr/bin/perl

use strict;
use warnings;

my @array  = ( 0 .. 6 );
my @array1 = ( ‘a‘ .. ‘d‘ );

my @replaced = splice( @array, 3 );

print "replaced:     @replaced\n",
      "resulting in: @array\n\n";

output:

replaced:     3 4 5 6
resulting in: 0 1 2

 
6、join 串連列表中的各個分離的串,產生一個新的串,返回一個標量!

#!/usr/bin/perl

use strict;
use warnings;

my @array = ( 0 .. 6 );
my $replaced = join("\n", @array);

print "$replaced\n",

output:

0
1
2
3
4
5
6

 
7、split
把字串進行分割並把分割後的結果放入數組中

 perl -le ‘$p=q(/var/ftp/test);@a=split(/\/ftp\//,$p);print $a[1];‘
test
 perl -le ‘$p=q(/var/ftp/test);@a=split(/\/ftp\//,$p);print $a[0];‘
/var

 

8、scalar
統計數組的長度,一般我們不用這個,直接將數組賦值給標量即可。

#!/usr/bin/perl

use strict;
use warnings;

my @array = ( 0 .. 6 );
my $count1 = @array;
my $count2 = scalar @array;

print "$count1\n";
print "$count2\n";

output:

7
7

 

9、sort
對數組元素進行排序

#!/usr/bin/perl

use strict;
use warnings;

my @array    = ( 0 .. 9 );
my @reversed = reverse @array;
print "Original:    @array\n";
print "Reversed:    @reversed\n\n";

# create an unsorted array of numbers and sort it
my @array2            = ( 100, 23, 9, 75, 5, 10, 2, 50, 7, 96, 1, 40 );
my @sortedLexically   = sort @array2;
my @sortedNumerically = sort { $a <=> $b } @array2;
print "Unsorted:    @array2\n";
print "Lexically:   @sortedLexically\n";
print "Numerically: @sortedNumerically\n";


output:

Original:    0 1 2 3 4 5 6 7 8 9
Reversed:    9 8 7 6 5 4 3 2 1 0

Unsorted:    100 23 9 75 5 10 2 50 7 96 1 40
Lexically:   1 10 100 2 23 40 5 50 7 75 9 96
Numerically: 1 2 5 7 9 10 23 40 50 75 96 100

 

Perl 數組應用詳解(push, pop, shift, unshift)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.