perl - 在perl脚本中拼接

标签 perl splice

my @writers = qw( Horace Ovid Virgil Asimov Heinlein Dante );
my @contemporary = splice @writers, 3, 2;
print join(' ', @contemporary);

输出

Asimov Heinlein


my @writers = qw( Horace Ovid Virgil Asimov Heinlein Dante );
splice @writers, 3, 2;
print join(' ', @writers); # Horace Ovid Virgil Dante

输出

Horace Ovid Virgil Dante

这些脚本有什么区别。为什么它显示不同的输出?

最佳答案

这里

 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

Removes the elements designated by OFFSET and LENGTH from an array, and replaces them with the elements of LIST, if any. In list context, returns the elements removed from the array. In scalar context, returns the last element removed, or undef if no elements are removed. The array grows or shrinks as necessary. If OFFSET is negative then it starts that far from the end of the array. If LENGTH is omitted, removes everything from OFFSET onward. If LENGTH is negative, removes the elements from OFFSET onward except for -LENGTH elements at the end of the array. If both OFFSET and LENGTH are omitted, removes everything. If OFFSET is past the end of the array, Perl issues a warning, and splices at the end of the array.

关于perl - 在perl脚本中拼接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20743698/

相关文章:

Perl:显示使用的子例程

perl - 重用计算出的 md5(或任何其他校验和)

python - 字符串中的连续值,获取索引

flash - 如何录制/拼接FLV片段?

javascript - 为什么我从这个简单的 splice 命令得到这个输出?

perl - 从 FTP 复制到 Windows 期间压缩数据丢失

Perl:引用相同的哈希定义哈希,$this->{key}?

mysql - DBI - Perl - 记录 MySQL 警告

Javascript:在数组中搜索重复项

Javascript - 如何从给定索引处的数组中删除项目