Manual

- Overview
- Tutorials
- FAQs
- History / Changes
- License

Reference

- Language
- Functions
- Operators
- Special Variables
- Pragmas
- Utilities
- Internals
- Platform Specific

Modules

- A • B • C • D • E
- F • G • H • I • L
- M • N • O • P • S
- T • U • X

News / Blogs

- Perl Buzz
- Perl Iron Man
- Perlsphere
- Planet Perl
- TPF news
- Use Perl

See also

- CPAN
- Perl.org
- Perl.com
- Perl 5 Wiki
- Perl Jobs
- Perl Mongers
- Perl Monks

Contact

- Site designed and maintained by Jon Allen (JJ)
- http://perl.jonallen.info

- View the project page and FAQs

# splice

Perl 5 version 10.1 documentation
Go to top

# splice

Perl functions A-Z | Perl functions by category | The 'perlfunc' manpage

- splice ARRAY,OFFSET,LENGTH,LIST

- splice ARRAY,OFFSET,LENGTH
- splice ARRAY,OFFSET
- splice ARRAY

  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 starts that far from the end of the array. If LENGTH is omitted, removes everything from OFFSET onward. If LENGTH is negative, removes the elements from OFFSET onward except for -LENGTH elements at the end of the array. If both OFFSET and LENGTH are omitted, removes everything. If OFFSET is past the end of the array, perl issues a warning, and splices at the end of the array.

  The following equivalences hold (assuming `$[ == 0 and $#a >= $i` )

          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[$i] = $y		splice(@a,$i,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)) { ... }

Recently read

Tools

- Preferences