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
打印为空