perl pop push shift unshift實(shí)例介紹
更新時(shí)間:2013年02月09日 22:54:15 作者:
perl的pop跟push操作數(shù)組的最右邊,shift跟unshift操作數(shù)組的最左邊
學(xué)習(xí)記錄一下
#!/usr/bin/perl -w
use strict;
print '@a @b @c @d 的值都為1 2 3',"\n";
my @a = (1..3);
pop(@a);
print "\@a的值為@a,pop拿掉數(shù)組最右邊的值\n";
my @b = (1..3);
push(@b,'4');
print "\@b的值為@b,push添加一個(gè)值到數(shù)組的最右邊。\n";
my @c = (1..3);
shift@c;
print "\@c的值為@c,shift拿掉數(shù)組最左邊的一個(gè)值。\n";
my @d = (1..3);
unshift(@d,0);
print "\@d的值為@d,unshift添加一個(gè)值到數(shù)組的最左邊。\n";
[root@OTRS perl]# perl pop_push_shift_unshift.pl
@a @b @c @d 的值都為1 2 3
@a的值為1 2,pop拿掉數(shù)組最右邊的值
@b的值為1 2 3 4,push添加一個(gè)值到數(shù)組的最右邊。
@c的值為2 3,shift拿掉數(shù)組最左邊的一個(gè)值。
@d的值為0 1 2 3,unshift添加一個(gè)值到數(shù)組的最左邊。
復(fù)制代碼 代碼如下:
#!/usr/bin/perl -w
use strict;
print '@a @b @c @d 的值都為1 2 3',"\n";
my @a = (1..3);
pop(@a);
print "\@a的值為@a,pop拿掉數(shù)組最右邊的值\n";
my @b = (1..3);
push(@b,'4');
print "\@b的值為@b,push添加一個(gè)值到數(shù)組的最右邊。\n";
my @c = (1..3);
shift@c;
print "\@c的值為@c,shift拿掉數(shù)組最左邊的一個(gè)值。\n";
my @d = (1..3);
unshift(@d,0);
print "\@d的值為@d,unshift添加一個(gè)值到數(shù)組的最左邊。\n";
[root@OTRS perl]# perl pop_push_shift_unshift.pl
@a @b @c @d 的值都為1 2 3
@a的值為1 2,pop拿掉數(shù)組最右邊的值
@b的值為1 2 3 4,push添加一個(gè)值到數(shù)組的最右邊。
@c的值為2 3,shift拿掉數(shù)組最左邊的一個(gè)值。
@d的值為0 1 2 3,unshift添加一個(gè)值到數(shù)組的最左邊。
相關(guān)文章
perl控制流介紹(if條件,while,for循環(huán),foreach)
Perl控制流(if條件,while,for循環(huán)),需要的朋友可以參考下2013-02-02perl高級(jí)排序,<=>操作符,飛船操作符
perl高級(jí)排序的關(guān)鍵在于要指定排序的方式,使用的操作符是spaceship operator(<=>)2013-02-02