splice

spliceARRAY,OFFSET,LENGTH,LISTspliceARRAY,OFFSET,LENGTHspliceARRAY,OFFSETspliceARRAY
This function removes the elements designated by
OFFSET and LENGTH
from an ARRAY, and replaces them with the
elements of LIST, if any. If
OFFSET is negative, the function counts
backward from the end of the array, but if that would land before the
beginning of the array, an exception is raised. If
LENGTH is negative, it removes the elements
from OFFSET onward except for –LENGTH elements at
the end of the array. If both OFFSET and
LENGTH are in list context, splice returns the elements removed from the
array. In scalar context, it returns the last element removed, or
undef if there was none. If the
number of new elements doesn’t equal the number of old elements, the
array grows or shrinks as necessary, and elements after the splice
change their position correspondingly. If
LENGTH is omitted, the function removes
everything from OFFSET onward. If
OFFSET is omitted, the array is cleared as it
is read. 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.
The equivalents listed in Table 27-5 hold.
Table 27-5. Splice equivalents for array operations
| Direct Method | Splice Equivalent |
|---|---|
push(@a, ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access