Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Remove the first element of an ...Prepend more elements to the b ... (Berikutnya)
Functions for real @ARRAYs

Add or remove elements anywhere in an array

Daftar Isi

  • splice ARRAY or EXPR,OFFSET,LENGTH,LIST

  • splice ARRAY or EXPR,OFFSET,LENGTH
  • splice ARRAY or EXPR,OFFSET
  • splice ARRAY or EXPR

    Removes the elements designated by OFFSET and LENGTH from an array, andreplaces 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 areremoved. 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 onwardexcept for -LENGTH elements at the end of the array.If both OFFSET and LENGTH are omitted, removes everything. If OFFSET ispast the end of the array, Perl issues a warning, and splices at theend of the array.

    The following equivalences hold (assuming $#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)) { ... }

    Starting with Perl 5.14, splice can take scalar EXPR, which must hold areference to an unblessed array. The argument will be dereferencedautomatically. This aspect of splice is considered highly experimental.The exact behaviour may change in a future version of Perl.

    To avoid confusing would-be users of your code who are running earlierversions of Perl with mysterious syntax errors, put this sort of thing atthe top of your file to signal that your code will work only on Perls ofa recent vintage:

    1. use 5.014;# so push/pop/etc work on scalars (experimental)
 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Remove the first element of an ...Prepend more elements to the b ... (Berikutnya)