在Perl6中向数组附加多个值

Mar*_*rth 9 arrays nested append flatten perl6

我正在寻找一种方法将多个值附加到@array.该文档指出,有一个名为.append的方法可以完成这项工作.但是当我做这样的事情时:

my @array = <a b>;
my @values = 1,2,3;
@array.append: @values, 17;
Run Code Online (Sandbox Code Playgroud)

我得到一个嵌套的结果:

[a b [1 2 3] 17]
Run Code Online (Sandbox Code Playgroud)

Eli*_*sen 11

你需要滑动数组,因为Perl 6不会自动滑动("展平"),除非它是参数中唯一可迭代的.

所以:

@array.append: @values;      # will slip the array as it's the only parameter
@array.append: @values,17;   # does not slip @values
@array.append: |@values, 17; # will slip the @values into @array
Run Code Online (Sandbox Code Playgroud)

相反的|@values,你也可以slip(@values)@values.Slip.