Appending multiple values to an array in Perl6
You need to slip the array as Perl 6 doesn't auto-slip ("flatten"), except if it's the only iterable in an a argument.
So:
@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
Instead of |@values
, you could also slip(@values)
or @values.Slip
.
This goes rather to explain what's going on there: Following the single argument rule, which applies here and basically says that whatever is passed to an iterator (append
in this case) is treated as a single argument, @values,17
is building a list (which would be the single argument), since ,
is the list building operator; append
is applied to every element of the list in turn, without flattening it: first to the arran @values
, second to the number.
Baseline is: if you handle an iterator stuff with commas, it will build a list out of that turning it into a single argument, instead of making it two arguments and doing
(@array.append: <first argument>).append( <second argument> )
So if you want to add everything as a single, flat, list, do as @lizmat says in her answer, or do a loop and append every element in turn.