Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: [splice], explanation please?

by bart (Canon)
on Nov 16, 2002 at 10:52 UTC ( #213388=note: print w/replies, xml ) Need Help??

in reply to [splice], explanation please?

Hmm... I was involved in a discussion on splice the other day, but I'm sure it wasn't with you... Anyway, IMO, splice() is one of the more difficult to use built-in functions in Perl.

OK, here's the basic gist: splice is a bit to arrays, what substr is to strings. You can cut out a section of an array, and replace it with something else. There's no way to just get an excerpt from an array, and leaving it alone: use an array slice for that. To add to the confusion, the two words look much alike, don't they? I'll come back to slices later.

So here's the basic syntax. We'll make an array containing 9 lower case letters. Then we'll extract two items, skipping the first 3, so taking the fourth and fifth item, and replace them with 3 other items, each an uppercase letter.

@a = ('a' .. 'i'); # 9 lower case letters print "Before: @a\n"; # prints: a b c d e f g h i @r = ('X', 'Y', 'Z'); # this is what we'll replace with... @x = splice @a, 3, 2, @r; # Bazaam! print "After: @a\n"; # prints: a b c X Y Z f g h i print "Pulled out: @x\n"; # prints: d e
Make sure you understand this before you attempt anything else.

The fourth parameter, the array @r, is evaluated in list context. So it's actually a list. We may substitute it with a list, containing arrays and/or scalars, and whatnot. The next snippet would produce the same result:

@x = splice @a, 3, 2, 'X', 'Y', 'Z';
which gently brings us to optional parameters: what if the list is empty?
@x = splice @a, 3, 2;
Well: the extracted slice will be replaced by an empty list. Of course.

What if we drop the third argument as well?

@x = splice @a, 3;
Well: now everything skipping the first 3 items, till the end of the array, will be extracted, and replaced by an empty list. So after that, @a will still contain the original first 3 items, and nothing else. It is not the same as taking a 0 for the third argument, which wouldn't do anything useful.

Till the end of the array is useful, but what if we want to keep a number of items, counting from the end of the array? Well: use a negative value for the third parameter.

@x = splice @a, 3, -2;
After that, starting out with the original @a, you'll get the results:
Before: a b c d e f g h i After: a b c h i Pulled out: d e f g
It left the original two last items alone. You can do the same with a list after this third parameter:
@x = splice @a, 3, -2, @r;
which will insert the values in @r where it extracted ('d', 'e', 'f', 'g') out. This also demonstrates a design error in splice(): there's no way to extract all items tiull the end of the array, and replace them with the contents of @r. 0 is not a negative number, and undef for the third parameter doesn't work.

Finally, you can use a negative value for the second parameter, the start index, as well. In that case, it will start counting from the end of the array.

@x = splice @a, -5, -1, @r;
You can still use a positive value for the third parameter here, with the usual meaning.

That sums it up, somewhat. As you can see, the rule is: splice() returns what is pulls out of the array, which is in turn replaced by an optional substitution list. What if you don't want to modify the original array? Then use an array slice, which syntactically is totally unrelated to the splice() function:

@x = @a[2..5];
@x = @a[2, 3, 4, 5];
This is quite the same as
@x = ($a[2], $a[3], $a[4], $a[5]);
You can use negative values for the index as well here, but you must remember that the range is evaluated independently of the context (i.e. knowing what it's used for) here, so combining negative and positive values for the index will not do what you probably want. (-5 .. 4) means (-5, -4, -3, -2, -1, 0, 1, 2, 3, 4), and (5 .. -2) is an empty list.

Replies are listed 'Best First'.
Re: Re: [splice], explanation please?
by hawtin (Prior) on Nov 16, 2002 at 15:15 UTC

    Excelent, a good explination of the function. In my opinion you should consider putting this in the "Tutorials" section.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://213388]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-07-23 22:14 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (347 votes). Check out past polls.