Perl Monk, Perl Meditation PerlMonks

### perlfunc:splice

by gods (Initiate)
 on Aug 24, 1999 at 22:42 UTC ( #275=perlfunc: print w/replies, xml ) Need Help??

# splice

See the current Perl documentation for splice.

Here is our local, out-dated (pre-5.6) version:

splice - add or remove elements anywhere in an array

splice ARRAY,OFFSET,LENGTH,LIST

splice ARRAY,OFFSET,LENGTH

splice ARRAY,OFFSET

Removes the elements designated by OFFSET and LENGTH from an array, and replaces them with the elements of LIST, if any. In list context, returns the elements removed from the array. In scalar context, returns the last element removed, or undef if no elements are removed. The array grows or shrinks as necessary. If OFFSET is negative then it start that far from the end of the array. If LENGTH is omitted, removes everything from OFFSET onward. If LENGTH is negative, leave that many elements off the end of the array. The following equivalences hold (assuming `\$[ == 0`):

```    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)
```

Example, assuming array lengths are passed before arrays:

```    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)) { ... }
```

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2023-12-03 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What's your preferred 'use VERSION' for new CPAN modules in 2023?

Results (20 votes). Check out past polls.

Notices?