8.26. splice

Removes the elements designated by OFFSET and LENGTH (and returns them).
Replaces them with the content of the 4th parameter.

Example 8-19. examples/arrays/splice.pl

#!/usr/bin/perl
use strict;
use warnings;

my @names = qw(Foo Bar Baz Moo Qux Barney Hoppy Bammbamm Dino);
my @more_names = qw(Fred Wilma);

my @sublist = splice(@names, 2, 3);
print "@sublist\n";  # Baz Moo Qux
print "@names\n";    # Foo Bar Barney Hoppy Bammbamm Dino

my @zlist = splice(@names, 2, 3, @more_names);
print "@zlist\n";    # Barney Hoppy Bammbamm
print "@names\n";    # Foo Bar Fred Wilma Dino

If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed