<?xml version="1.0" encoding="windows-1252"?>
<node id="275" title="perlfunc:splice" created="1999-08-24 18:42:48" updated="2005-08-14 22:51:13">
<type id="119">
perlfunc</type>
<author id="114">
gods</author>
<data>
<keywords>
<keyword rating="">
dice</keyword>
</keywords>
<field name="doctext">
</field>
<field name="name">

&lt;P&gt;
splice - add or remove elements anywhere in an array

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="synopsis">

&lt;P&gt;
splice 
&lt;FONT SIZE=-1&gt;ARRAY,OFFSET,LENGTH,LIST&lt;/FONT&gt;

&lt;P&gt;
splice 
&lt;FONT SIZE=-1&gt;ARRAY,OFFSET,LENGTH&lt;/FONT&gt;

&lt;P&gt;
splice 
&lt;FONT SIZE=-1&gt;ARRAY,OFFSET&lt;/FONT&gt;

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="description">

&lt;P&gt;
Removes the elements designated by 
&lt;FONT SIZE=-1&gt;OFFSET&lt;/FONT&gt; and 
&lt;FONT SIZE=-1&gt;LENGTH&lt;/FONT&gt; from an array, and replaces them with the elements of 
&lt;FONT SIZE=-1&gt;LIST,&lt;/FONT&gt; 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 
&lt;FONT SIZE=-1&gt;OFFSET&lt;/FONT&gt; is negative then it start that far from the end of the array. If 
&lt;FONT SIZE=-1&gt;LENGTH&lt;/FONT&gt; is omitted, removes everything from 
&lt;FONT SIZE=-1&gt;OFFSET&lt;/FONT&gt; onward. If 
&lt;FONT SIZE=-1&gt;LENGTH&lt;/FONT&gt; is negative, leave that many elements off the end of the array. The following equivalences hold (assuming
 &lt;CODE&gt;$&amp;#091; == 0&lt;/CODE&gt;):

&lt;P&gt;
&lt;PRE&gt;    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&amp;#091;$x&amp;#093; = $y         splice(@a,$x,1,$y)
&lt;/PRE&gt;
&lt;P&gt;
Example, assuming array lengths are passed before arrays:

&lt;P&gt;
&lt;PRE&gt;    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 (&amp;amp;aeq($len,@foo&amp;#091;1..$len&amp;#093;,0+@bar,@bar)) { ... }
&lt;/PRE&gt;
&lt;HR&gt;
</field>
</data>
</node>
