routine splice
Documentation for routine splice
assembled from the following types:
role Buf
From Buf
(Buf) method splice
method splice( Buf: = 0, ?, * --> Buf)
Substitutes elements of the buffer by other elements
.splice: 0, 3, <3 2 1>;say .perl; # OUTPUT: «Buf.new(3,2,1,2,3,5,8,13,21,34,55,89)»
class Array
From Array
(Array) routine splice
Defined as:
multi sub splice(, = 0, ?, * --> Array)multi method splice(Array: = 0, ?, * --> Array)
Deletes $elems
elements starting from index $start
from the Array
, returns them and replaces them by @replacement
. If $elems
is omitted or is larger than the number of elements starting from $start
, all the elements starting from index $start
are deleted. If both $start
and $elems
are omitted, all elements are deleted from the Array
and returned.
Each of $start
and $elems
can be specified as a Whatever or as a Callable that returns an int
-compatible value.
A Whatever
$start
uses the number of elements of @list
(or invocant). A Callable
$start
is called with one argument—the number of elements in @list
—and its return value is used as $start
.
A Whatever
$elems
deletes from $start
to end of @list
(same as no $elems
). A Callable
$elems
is called with just one argument—the number of elements in @list
minus the value of $start
—and its return value is used the value of $elems
.
Example:
my = <a b c d e f g>;say .splice(2, 3, <M N O P>); # OUTPUT: «[c d e]»say ; # OUTPUT: «[a b M N O P f g]»