Skip navigation.
Your source for Perl tips, howto's, faq and tutorials
( categories: )

Use the 'splice' function. The syntax is:

splice @array, $offset, $length, @list


  • @array is the target array
  • $offset is the starting array element to remove. The first element is 0. If '$offset' is negative it starts counting from the end of the array
  • $length is the number of array elements to remove
  • @list (optional) is a list of elements that will replace the element removed

return value:

In scalar context, 'splice' returns the last element removed. In list context returns the list of elements removed from the array.


@superheroes = qw(batman superman supergirl wonder_woman batgirl spiderman);
@villains = qw(luthor joker venom);
#-- remove females and keep them in their own list
@females = splice(@superheroes, 2, 3);
#-- remove superman and replace it with villains
splice(@superheroes, 1, 1, @villains);