|Perl: the Markov chain saw|
Re: [splice], explanation please?by bart (Canon)
|on Nov 16, 2002 at 10:52 UTC||Need Help??|
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.
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:
which gently brings us to optional parameters: what if the list is empty?
Well: the extracted slice will be replaced by an empty list. Of course.
What if we drop the third argument as well?
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.
After that, starting out with the original @a, you'll get the results:
It left the original two last items alone. You can do the same with a list after this third parameter:
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.
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:
This is quite the same as
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.