splice ARRAY, OFFSET, LENGTH, LIST
splice ARRAY, OFFSET, LENGTH
splice ARRAY, OFFSET
This function removes the elements designated by OFFSET and LENGTH fro
+m an array, and replaces them with the elements of LIST, if any. The
+function returns the elements removed from the array. The array grows
+ or shrinks as necessary. If LENGTH is omitted, the function removes
+everything from OFFSET onward. The following equivalences hold (assum
+ing $[ is 0):
Direct Method Splice Equivalent
---------------------------------------------------
push(@a, $x, $y) | splice(@a, $#a+1, 0, $x, $y)
pop(@a) | splice(@a, -1)
shift(@a) | splice(@a, 0, 1)
unshift(@a, $x, $y) | splice(@a, 0, 0, $x, $y)
$a[$x] = $y | splice(@a, $x, 1, $y);
The splice function is also handy for carving up the argument list pas
+sed to a subroutine. For example, assuming list lengths are passed be
+fore lists:
sub list_eq { # compare two list values
my @a = splice(@_, 0, shift);
my @b = splice(@_, 0, shift);
return 0 unless @a == @b; # same len?
while (@a) {
return 0 if pop(@a) ne pop(@b);
}
return 1;
}
if (list_eq($len, @foo[1..$len], scalar(@bar), @bar)) { ... }
It would probably be cleaner just to use references for this, however.
-
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.