perl-5.38.0
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 and a LENGTH was provided, Perl issues a warning, and splices at the end of the array.

ARRAY から OFFSET、LENGTH で指定される要素を取り除き、 LIST があれば、それを代わりに挿入します。 リストコンテキストでは、配列から取り除かれた要素を返します。 スカラコンテキストでは、取り除かれた最後の要素を返します; 要素が 取り除かれなかった場合は undef を返します。 配列は、必要に応じて、大きくなったり、小さくなったりします。 OFFSET が負の数の場合は、配列の最後からの距離を示します。 LENGTH が省略されると、OFFSET 以降のすべての要素を取り除きます。 LENGTH が負の数の場合は、OFFSET から前方へ、配列の最後から -LENGTH 要素を 除いて取り除きます。 OFFSET と LENGTH の両方が省略されると、全ての要素を取り除きます。 OFFSET が配列の最後より後ろで、 LENGTH が指定されていると、Perl は警告を出し、 配列の最後に対して処理します。

The following equivalences hold (assuming $#a >= $i )

以下は、($#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)

splice can be used, for example, to implement n-ary queue processing:

splice は、例えば、n-ary キュー処理の 実装に使えます:

    sub nary_print {
      my $n = shift;
      while (my @next_n = splice @_, 0, $n) {
        say join q{ -- }, @next_n;
      }
    }

    nary_print(3, qw(a b c d e f g h));
    # prints:
    #   a -- b -- c
    #   d -- e -- f
    #   g -- h

Starting with Perl 5.14, an experimental feature allowed splice to take a scalar expression. This experiment has been deemed unsuccessful, and was removed as of Perl 5.24.

Perl 5.14 から、splice がスカラ式を 取ることが出来るという実験的機能がありました。 この実験は失敗と見なされ、Perl 5.24 で削除されました。