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 :)

December 03, 2014, 15:24

Best Android Stuffs Android News, Rooting, Firmwares, Custom ROMs, Android Apps

December 03, 2014, 15:24

Best Android Stuffs Android News, Rooting, Firmwares, Custom ROMs, Android Apps

Kaur deep Permalink
December 04, 2014, 03:06

Excellent and knowledgeable links and post.thanks for sharing.
Now, The Time has come for the Happy New Year 2015. It wil bring a lot fo happinesin our life.
Happy New Year 2015
And I am sure, You wil anjoy this new year day with yoru fiends and wil send some images wishes,
New Year 2015

Happy New Year 2015 Wishes
Greetings to yoru close ones, Your friends, You lover, etc..
Happy New Year 2015 Images
Even I am waititng for that day, because it just changes our life.
Happy New Year 2015 Greetings

Happy New Year 2015 SMS
So, That's why I am commenting here to wish you a very happy new year.
Happy New Year 2015 Messages
I hope this New year 2015 wil bring success to your website. But before that Merry Christmas is coming.
Merry Christmas Quotes
It is also a big event for everyone.
Merry Christmas Pictures
Everyone knows that Christmas is a day when People seems to be very happy, they just leave all sadness behind.
Merry Christmas Wallpapers
And I am sure, you also enjoy this Merry Christmas day of 2014.
Christmas day Images

Christmas stocking
But if you are not looking out to enjoy this christmas, then you are missing out on a big thing, because it really brings happiness.
Merry Christmas Images

Merry Christmas 2014
So, what you are waiting for?
Merry Christmas 2015
Enjoy This Merry Christmas day. Cheers :).

shu Permalink
December 11, 2014, 07:34

superb Happy new Year 2015
Thanks

Merry Christmas Permalink
December 08, 2014, 14:59
Diana Permalink
December 17, 2014, 15:47

This article is quite helpful and informative too. I enjoyed a lot. Thanks for sharing such a great article.

Beautiful Christmas Quotes for your friends and family...
christmas quotes
Christmas Messages for Whatsapp

Best Christmas Greetings for your friends and family...
christmas greeting
christmas wishes

What to Write in Christmas Card....... Check out best Christmas Greeting Card Words
What to Write in Christmas Card

GetBeautiful and Unique Christmas Wallpapers for free
free christmas wallpaper
christmas tree decorating ideas

Thanks for sharing such a great article.

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 "coding_429": (just to make sure you're a human)

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

Advertisements