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 @a:

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 @a 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 @a:

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

Then you can pop it via splice like this:

splice(@a, -1)

The @a 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 @a:

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

Then you can shift a value like this:

splice(@a, 0, 1)

The @a 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 @a:

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 @a 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 @a:

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 @a 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 @a 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.

Comments

rektide Permalink
July 13, 2014, 15:11

I wrote a small library to do the same thing in JavaScript- implement array methods in terms of splice. I needed an array-like object, and this technique made it much easier to implement.
https://github.com/rektide/splicer

Thanks for the post, nice read.

You can't extremely say what exactly is stunning a few put, however typically the image on the put can stay vividly along with youI was terribly encouraged to locate this web site. I needed to thanks a lot involving this special browse. I undoubtedly savored each amount of it and I maybe you have bookmarked to try out new stuff you writeup.

July 19, 2014, 02:11

Thank you so much for the information you have shared with us!

August 12, 2014, 11:56

This is really nice update sir.

Regards,
Resultswala.

August 19, 2014, 16:30

Wide Venture - Breaking News from India
Thanks for sharing such a great article.

August 22, 2014, 06:51

Thanks for such a nice posting. I have you bookmarked your site to check out new stuff you post.
Printsfab, Printsfab Jaipur, Printsfab Online Shopping Jaipur, Online Shopping Women Legging, Dresses, Kurtis, Patiala, Dupatta, Tops, Chudidar in Jaipur, Custom T Shirts printing for Man in Jaipur, Kurtis and Patiala Dupatta Online Shopping Jaipur, Online Designer Dresses shopping In Jaipur, printed cotton T Shirt Online Shopping Jaipur, printed top 100% cotton online Shopping Jaipur India, Chudidar online shopping in Jaipur.

August 22, 2014, 06:52

Thanks for such a nice posting. I have you bookmarked your site to check out new stuff you post.
Printsfab, Printsfab Jaipur, Printsfab Online Shopping Jaipur, Online Shopping Women Legging, Dresses, Kurtis, Patiala, Dupatta, Tops, Chudidar in Jaipur, Custom T Shirts printing for Man in Jaipur, Kurtis and Patiala Dupatta Online Shopping Jaipur, Online Designer Dresses shopping In Jaipur, printed cotton T Shirt Online Shopping Jaipur, printed top 100% cotton online Shopping Jaipur India, Chudidar online shopping in Jaipur.

Happy christmas quotes keep you have really good website i will back again

November 13, 2014, 04:43

I am very glad to read this great post.this is very useful and helpful for us.i really appreciate you for your good work.
thanks for sharing.
regards.
M.jamshaid.
http://sms4like.com/romantic-good-night-sms-wife/

roger Permalink
November 17, 2014, 04:54

rowe gi bajo kale chalia cabbe gi

cool and amazing sir, to be continue :)

November 23, 2014, 14:03

thank you for sharing... viber for pc.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "cloud": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements