Someone asked me today about Perl array operations and it turned out he didn't know that all basic array operations, such as push, pop, shift, unshift can be implemented with just splice. So I thought I'd write a blog post that shows how awesome splice is.

splice

Splice has the following prototype:

splice(array, [offset, [length, [list]]])

Splice replaces length elements in array at offset with list. Offset, length, and list can be omitted, which makes it a really powerful function that can do a bunch of array operations.

push via splice

To push to an array via splice, you need to set offset to the end of the array, length to anything, and list to the list of values that you want to add.

Let's say you've array code>@a</code:

my @a = ("foo", "bar", "baz", "rms", "lbt", "esr");

Then you can push to it via splice like this:

splice(@a, @a, 0, "val1", "val2", "val3")

The code>@a</code array now contains:

("foo", "bar", "baz", "rms", "lbt", "esr", "val1", "val2", "val3")

pop via splice

To pop an element from an array via splice, you need to set offset to the end of the array.

Let's say you've array code>@a</code:

my @a = ("foo", "bar", "baz", "rms", "lbt", "esr");

Then you can pop it via splice like this:

splice(@a, -1)

The code>@a</code array now contains:

("foo", "bar", "baz", "rms", "lbt")

Here splice replaced the last element of the array with nothing, effectively removing it.

shift via splice

To shift a value off an array via splice, you need to set offset to the beginning of the array, and length to 1.

Let's say you've array code>@a</code:

my @a = ("foo", "bar", "baz", "rms", "lbt", "esr");

Then you can shift a value like this:

splice(@a, 0, 1)

The code>@a</code array now contains:

("bar", "baz", "rms", "lbt", "esr")

Similarly you can shift more than one value, if you increase length:

splice(@a, 0, 5)

This shifts first 5 values off the original array and it now contains:

("esr")

unshift via splice

To unshift values to an array via splice, you need to set offset to the beginning of the array, length to 0, and list to the list of values that you want to add.

Let's say you've array code>@a</code:

my @a = ("foo", "bar", "baz", "rms", "lbt", "esr");

Then you can unshift a new list to this array like this:

splice(@a, 0, 0, "val1", "val2")

The code>@a</code array now contains:

("val1", "val2", "foo", "bar", "baz", "rms", "lbt", "esr")

replace i-th element in an array via splice

To replace i-th element in an array via splice, you need to set offset to i, length to 1 and list to the list of values.

Let's say you've array code>@a</code:

my @a = ("foo", "bar", "baz", "rms", "lbt", "esr");

Then you can replace the 2nd element ("bar") via splice like this:

splice(@a, 1, 1, "ror")

The code>@a</code array now contains:

("foo", "ror", "baz", "rms", "lbt", "esr")

Similarly you can replace the i-th element with a list of values:

splice(@a, 1, 2, "ror", "zoz")

The code>@a</code array now contains:

("foo", "ror", "zoz", "baz", "rms", "lbt", "esr")

There are more operations that you can do with splice, such as deleting all elements in the array, adding elements at the i-th position. I'll leave these other operations as an exercise to the reader.

I think you can do the same in other languages as well, such as JavaScript, as it also has array.splice operation.