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", "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.

mimi Permalink
July 14, 2014, 11:18

the jogos de cozinhar are very well to play as they are great all over

mimi Permalink
July 14, 2014, 11:20

the jogos de vestir are like teh actuall dress up games that can be played

July 15, 2014, 12:51

Actually this post is about my personal interest and the mid of the post is truly informative and I love and elaboration quality of writer. I always try to find this type of info regarding this topic now here I got this thanks Happy Friendship Day SMS Friendship Day SMS Independence Day Images Happy Independence Day Images Independence Day Images India India Independence Day again thanks for this great blog.

July 16, 2014, 05:53

In the winter 2014/15 ready to wear fashion, shoes not only can match with suits, but also can be used with narrow leg pants, coat or gown complement gucci replica each other. It is with this fashion hermes replica spirit, upper or not, even with sports shoes and lace boots, form twenty kinds of different materials and colors: (herringbone twill weave, ring, etc.) a variety of effects of tweed, suede, laminate, crack hublot replica or iridescent sheen leather, black and pink, orange, black and gold, red and green, pink and silver...... Colorful living color, make the conference times with luxurious supermarket Chanel shopping center as the background chanel replica theme of vitality. The series of sports shoes will be formally launched in Chanel boutique in 2014 September.

----

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.

commonwealth Permalink
July 17, 2014, 08:42
July 19, 2014, 02:11

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

July 25, 2014, 13:15

Happy Friendship Day Quotes SMS 2014

Happy Friendship Day SMS Messages 140 Words/Characters 2014 | English

Happy Friendship Day Quotes SMS & Messages | Happy Friendship Day 2014

Happy Friendship Day Funny Quotes | Funny Friendship Day Quotes

Happy Friendship Day Quotes in Hindi | Kannada | Bengali | Telugu | Tamil

Happy Friendship Day SMS & Messages in Hindi | Happy Friendship Day

Happy Friendship Day Quotes in Hindi | Best Friendship Quotes

Happy Friendship Day Facebook Messages 2014 | Best FB Messages

Happy Friendship Day HD Wallpapers 2014 | Best Friendship Day Wallpapers

Happy Friendship Day Songs & Poems 2014 | Best Friendship Songs & Poems

Happy Friendship Day Quotes 2014 for Facebook | Best Friendship Quotes

Happy Friendship Day Poems 2014 | Best Friendship Day Poems

Happy Friendship Day SMS & Messages 2014 | Best Friendship SMS & Messages

Happy Friendship Day Whatsapp Status Quotes 2014 | Friendship Day Quotes

Happy Friendship Day SMS Messages 140 Words/Characters 2014

Happy Friendship Day Quotes 2014 | Best Friendship Day Quotes

Happy Friendship Day SMS Messages 140 Words/Characters 2014 | English

Happy Friendship Day Quotes SMS & Messages | Happy Friendship Day 2014

Happy Friendship Day Funny Quotes | Funny Friendship Day Quotes

Happy Friendship Day Quotes in Hindi | Kannada | Bengali | Telugu | Tamil

Happy Friendship Day SMS & Messages in Hindi | Happy Friendship Day

Happy Friendship Day Quotes in Hindi | Best Friendship Quotes

Happy Friendship Day Facebook Messages 2014 | Best FB Messages

Happy Friendship Day HD Wallpapers 2014 | Best Friendship Day Wallpapers

Happy Friendship Day Songs & Poems 2014 | Best Friendship Songs & Poems

Happy Friendship Day Quotes 2014 for Facebook | Best Friendship Quotes

Happy Friendship Day Poems 2014 | Best Friendship Day Poems

Happy Friendship Day SMS & Messages 2014 | Best Friendship SMS & Messages

Happy Friendship Day Whatsapp Status Quotes 2014 | Friendship Day Quotes

Happy Friendship Day Quotes 2014 | Best Friendship Day Quoteshere

July 25, 2014, 14:45

Actually this post is about my personal interest and the mid of the post is truly informative and I love and elaboration quality of writer. I always try to find this type of info regarding this topic now here I got this thanks Happy Friendship Day SMS Friendship Day SMS Friendship Day Happy Friendship Day Independence Day Images Happy Independence Day Images Independence Day Images India India Independence Day independence day speech independence day sms Independence day wallpaper independence day message independence day songs again thanks for this great blog.

Leave a new comment

(why do I need your e-mail?)

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

Type the first letter of your name: (just to make sure you're a human)

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

Advertisements