Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) 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.

@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];
or
@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.

In reply to Re: [splice], explanation please? by bart
in thread [splice], explanation please? by @rocks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-12-09 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?