splice有四种用法,分别是:
1.splice ARRAY,OFFSET,LENGTH,LIST
2.splice ARRAY,OFFSET,LENGTH
3.splice ARRAY,OFFSET
4.splice ARRAY
说明:移除在ARRAY中由OFFSET和LENGTH指定的元素,同时让LIST中的元素代替移除的元素。在列表上下文,函数返回溢出后的数组元素。在标量上下文,返回溢出的最后一个元素,如果无元素移除返回UNDEF。
注意:如果OFFSET为负数,则以数组的尾部为起始点。如果省略LENGTH(属于第三种情况),则移除从OFFSET到尾部的元素。如果LENGTH为负数,则除数组尾部LENGTH个元素外,移除从OFFSET到尾部的所有元素。如果OFFSET和LENGTH都为空,去移除所有元素。如果OFFSET越界,PERL会提示警告并在数组尾部中插入LIST或空。
1.splice ARRAY,OFFSET,LENGTH,LIST
#!/usr/bin/perl -w
use strict;
my @rocks = qw(talc quartz jade obsidian);
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.splice ARRAY,OFFSET,LENGTH
#!/usr/bin/perl -w
use strict;
my @rocks = qw(talc quartz jade obsidian);
my @tmp = qw(hell oworld);
splice(@rocks,1,2);
foreach (@rocks){
print $_."##";
}
print "\n";
[root@localhost ~]# perl refs.pl
talc##obsidian##
3.splice ARRAY,OFFSET
#!/usr/bin/perl -w
use strict;
my @rocks = qw(talc quartz jade obsidian);
my @tmp = qw(hell oworld);
splice(@rocks,2);
foreach (@rocks){
print $_."##";
}
print "\n";
[root@localhost ~]# perl refs.pl
talc##quartz##
4.splice ARRAY
#!/usr/bin/perl -w
use strict;
my @rocks = qw(talc quartz jade obsidian);
my @tmp = qw(hell oworld);
splice(@rocks);
foreach (@rocks){
print $_."##";
}
print "\n";
[root@localhost ~]# perl refs.pl
打印为空
阅读(1623) | 评论(0) | 转发(0) |