my @writers = qw( Horace Ovid Virgil Asimov Heinlein Dante );
my @contemporary = splice @writers, 3, 2;
print join(' ', @contemporary);输出
Asimov Heinleinmy @writers = qw( Horace Ovid Virgil Asimov Heinlein Dante );
splice @writers, 3, 2;
print join(' ', @writers); # Horace Ovid Virgil Dante输出
Horace Ovid Virgil Dante这些脚本之间有什么区别。为什么它表现出不同的输出?
发布于 2013-12-23 12:48:12
这里
my @contemporary = splice @writers, 3, 2;
print join(' ', @contemporary);它返回已删除的元素。
在这里
splice @writers,3,2;
print join(' ', @writers)它只打印数组的元素。
例如,在标量上下文中
my $contemporary = splice @writers, 3, 2;
print $contemporary;它将打印已删除的最后一个元素。
来自perldoc
从数组中移除由偏移量和长度指定的元素,并将它们替换为LIST元素(如果有的话)。在列表上下文中返回从数组中删除的元素。在标量上下文中,返回删除的最后一个元素,如果没有删除元素,则返回undef。数组在必要时增长或缩小。如果偏移量为负值,则从远离数组末尾的地方开始。如果省略长度,则从偏移开始删除所有内容。如果长度为负数,则从偏移量中移除数组末尾的-LENGTH元素以外的元素。如果省略偏移量和长度,则删除所有内容。如果偏移量超过数组的末尾,Perl将发出警告,并在数组的末尾连接。
https://stackoverflow.com/questions/20743698
复制相似问题