perlfunc
gods
dice
<P>
splice - add or remove elements anywhere in an array
<P>
<HR>
<P>
splice
<FONT SIZE=-1>ARRAY,OFFSET,LENGTH,LIST</FONT>
<P>
splice
<FONT SIZE=-1>ARRAY,OFFSET,LENGTH</FONT>
<P>
splice
<FONT SIZE=-1>ARRAY,OFFSET</FONT>
<P>
<HR>
<P>
Removes the elements designated by
<FONT SIZE=-1>OFFSET</FONT> and
<FONT SIZE=-1>LENGTH</FONT> from an array, and replaces them with the elements of
<FONT SIZE=-1>LIST,</FONT> if any. In list context, returns the elements removed from the array. In scalar context, returns the last element removed, or
[perlfunc:undef|undef] if no elements are removed. The array grows or shrinks as necessary. If
<FONT SIZE=-1>OFFSET</FONT> is negative then it start that far from the end of the array. If
<FONT SIZE=-1>LENGTH</FONT> is omitted, removes everything from
<FONT SIZE=-1>OFFSET</FONT> onward. If
<FONT SIZE=-1>LENGTH</FONT> is negative, leave that many elements off the end of the array. The following equivalences hold (assuming
<CODE>$[ == 0</CODE>):
<P>
<PRE> push(@a,$x,$y) splice(@a,@a,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)
</PRE>
<P>
Example, assuming array lengths are passed before arrays:
<P>
<PRE> sub aeq { # 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 (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... }
</PRE>
<HR>