perl-5.38.0
delete EXPR

Given an expression that specifies an element or slice of a hash, delete deletes the specified elements from that hash so that exists on that element no longer returns true. Setting a hash element to the undefined value does not remove its key, but deleting it does; see exists.

ハッシュの要素やスライスを指定する式を取り、delete は 指定された要素をハッシュから削除するので、 その要素に対する exists はもはや真を返さなくなります。 ハッシュ要素に未定義値をセットしてもそのキーは削除されませんが、 delete では削除されます; exists を参照してください。

In list context, usually returns the value or values deleted, or the last such element in scalar context. The return list's length corresponds to that of the argument list: deleting non-existent elements returns the undefined value in their corresponding positions. Since Perl 5.28, a key/value hash slice can be passed to delete, and the return value is a list of key/value pairs (two elements for each item deleted from the hash).

リストコンテキストでは通常は削除された要素を返し、スカラコンテキストでは 削除された要素のうち最後のものを返します。 返されたリストの長さは常に引数リストの長さに対応します: 存在しない要素を削除すると、対応する位置に未定義値をセットして返します。 Perl 5.28 から、 キー/値ハッシュスライスdelete に渡すことができ、 そして返り値はキー/値の組(それぞれのアイテムについて 二つの要素が元のハッシュから削除されたもの)です。

delete may also be used on arrays and array slices, but its behavior is less straightforward. Although exists will return false for deleted entries, deleting array elements never changes indices of existing values; use shift or splice for that. However, if any deleted elements fall at the end of an array, the array's size shrinks to the position of the highest element that still tests true for exists, or to 0 if none do. In other words, an array won't have trailing nonexistent elements after a delete.

delete は配列や配列のスライスに対しても使えますが、その 振る舞いはあまり直感的ではありません。 削除されたエントリに対しては exists は偽を返しますが、 配列要素を削除しても、存在する値の添え字は変わりません; このためには shiftsplice を 使ってください。 しかし、削除された要素が配列の末尾であった場合、配列のサイズは exists が真となる最大位置の要素(それがない場合は 0)に 切り詰められます。 言い換えると、delete の後には配列の末尾に値のない要素はありません。

WARNING: Calling delete on array values is strongly discouraged. The notion of deleting or checking the existence of Perl array elements is not conceptually coherent, and can lead to surprising behavior.

警告: 配列の値に対して delete を呼び出すことは強く 非推奨です。 Perl の配列要素を削除したり存在を調べたりする記法は概念的に一貫しておらず、 驚くべき振る舞いを引き起こすことがあります。

Deleting from %ENV modifies the environment. Deleting from a hash tied to a DBM file deletes the entry from the DBM file. Deleting from a tied hash or array may not necessarily return anything; it depends on the implementation of the tied package's DELETE method, which may do whatever it pleases.

%ENV から削除を行なうと、実際に環境変数を変更します。 DBM ファイルに tie された配列からの削除は、その DBM ファイルからエントリを 削除します。 しかし、tie されたハッシュや配列からの 削除は、値を返すとは限りません; これは tie されたパッケージの DELETE メソッドの実装に依存するので、どんなことでも起こります。

The delete local EXPR construct localizes the deletion to the current block at run time. Until the block exits, elements locally deleted temporarily no longer exist. See "Localized deletion of elements of composite types" in perlsub.

delete local EXPR 構文は、現在のブロックの削除を実行時にローカル化します。 ブロックから出るまで、ローカルで削除された要素は存在しなくなります。 "Localized deletion of elements of composite types" in perlsub を 参照してください。

    my %hash = (foo => 11, bar => 22, baz => 33);
    my $scalar = delete $hash{foo};         # $scalar is 11
    $scalar = delete @hash{qw(foo bar)}; # $scalar is 22
    my @array  = delete @hash{qw(foo baz)}; # @array  is (undef,33)

The following (inefficiently) deletes all the values of %HASH and @ARRAY:

以下は、%HASH と @ARRAY のすべての値を(非効率的に)削除します:

    foreach my $key (keys %HASH) {
        delete $HASH{$key};
    }

    foreach my $index (0 .. $#ARRAY) {
        delete $ARRAY[$index];
    }

And so do these:

そして以下のようにもできます:

    delete @HASH{keys %HASH};

    delete @ARRAY[0 .. $#ARRAY];

But both are slower than assigning the empty list or undefining %HASH or @ARRAY, which is the customary way to empty out an aggregate:

しかし、これら二つは両方とも、構造を空にするための慣習的な方法である、 単に空リストを代入するか、%HASH や @ARRAY を undef するより遅いです:

    %HASH = ();     # completely empty %HASH
    undef %HASH;    # forget %HASH ever existed

    @ARRAY = ();    # completely empty @ARRAY
    undef @ARRAY;   # forget @ARRAY ever existed

The EXPR can be arbitrarily complicated provided its final operation is an element or slice of an aggregate:

最終的な操作が集合の要素かスライスである限りは、 いずれかである限りは、EXPR には任意の複雑な式を置くことができます:

    delete $ref->[$x][$y]{$key};
    delete $ref->[$x][$y]->@{$key1, $key2, @morekeys};

    delete $ref->[$x][$y][$index];
    delete $ref->[$x][$y]->@[$index1, $index2, @moreindices];