gpt4 book ai didi

perl - 在perl脚本中拼接

转载 作者:行者123 更新时间:2023-12-04 18:08:19 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com