轉自:http://blog.chinaunix.net/u/9861/showart_727153.html
PERL函數splice的用法:
splice有四種用法,分別是:
1.spliceARRAY,OFFSET,LENGTH,LIST
2.spliceARRAY,OFFSET,LENGTH
3.spliceARRAY,OFFSET
4.spliceARRAY
說明:移除在ARRAY中由OFFSET和LENGTH指定的元素,同時讓LIST中的元素代替移除的元素。在列表上下文,函數返回溢出後的數組元素。在標量上下文,返回溢出的最後一個元素,如果無元素移除返回UNDEF。
注意:如果OFFSET為負數,則以數組的尾部為起始點。如果省略LENGTH(屬於第三種情況),則移除從OFFSET到尾部的元素。如果LENGTH為負數,則除數組尾部LENGTH個元素外,移除從OFFSET到尾部的所有元素。如果OFFSET和LENGTH都為空白,去移除所有元素。如果OFFSET越界,PERL會提示警告並在數組尾部中插入LIST或空。
1.spliceARRAY,OFFSET,LENGTH,LIST
#!/usr/bin/perl -w
use strict; my @rocks = qw(talc quartz jadeobsidian); my @tmp = qw(hell oworld); splice(@rocks,1,2,@tmp); foreach (@rocks){ print$_."##"; } print "\n";
|
[root@localhost ~]#perl refs.pl
talc##hell##oworld##obsidian##
2.spliceARRAY,OFFSET,LENGTH
#!/usr/bin/perl -w
use strict; my @rocks = qw(talc quartz jadeobsidian); my @tmp = qw(hell oworld); splice(@rocks,1,2); foreach (@rocks){ print$_."##"; } print "\n";
|
[root@localhost ~]# perlrefs.pl
talc##
3.splice ARRAY,OFFSET
#!/usr/bin/perl -w
use strict; my @rocks = qw(talc quartz jadeobsidian); my @tmp = qw(hell oworld); splice(@rocks,2); foreach (@rocks){ print$_."##"; } print "\n";
|
[root@localhost ~]# perlrefs.pl
talc##quartz##
4.splice ARRAY
#!/usr/bin/perl -w
use strict; my @rocks = qw(talc quartz jadeobsidian); my @tmp = qw(hell oworld); splice(@rocks); foreach (@rocks){ print$_."##"; } print "\n";
|
[root@localhost ~]# perlrefs.pl
列印為空白