Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

splice

Perl 5 version 8.9 documentation
Recently read

splice

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

    1. push(@a,$x,$y) splice(@a,@a,0,$x,$y)
    2. pop(@a) splice(@a,-1)
    3. shift(@a) splice(@a,0,1)
    4. unshift(@a,$x,$y) splice(@a,0,0,$x,$y)
    5. $a[$i] = $y splice(@a,$i,1,$y)

    Example, assuming array lengths are passed before arrays:

    1. sub aeq { # compare two list values
    2. my(@a) = splice(@_,0,shift);
    3. my(@b) = splice(@_,0,shift);
    4. return 0 unless @a == @b; # same len?
    5. while (@a) {
    6. return 0 if pop(@a) ne pop(@b);
    7. }
    8. return 1;
    9. }
    10. if (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... }