perl-5.38.0
each HASH
each ARRAY

When called on a hash in list context, returns a 2-element list consisting of the key and value for the next element of a hash. In Perl 5.12 and later only, it will also return the index and value for the next element of an array so that you can iterate over it; older Perls consider this a syntax error. When called in scalar context, returns only the key (not the value) in a hash, or the index in an array.

ハッシュに対してリストコンテキストで呼び出した場合は、次の要素に対する、 ハッシュのキーと値を返します。 Perl 5.12 以降でのみ、配列のインデックスと値からなる 2 要素のリストを返すので、反復を行えます; より古い Perl ではこれは 文法エラーと考えられます。 スカラコンテキストで呼び出した場合は、 ハッシュの場合は(値ではなく)キー、配列の場合はインデックスを返します。

Hash entries are returned in an apparently random order. The actual random order is specific to a given hash; the exact same series of operations on two hashes may result in a different order for each hash. Any insertion into the hash may change the order, as will any deletion, with the exception that the most recent key returned by each or keys may be deleted without changing the order. So long as a given hash is unmodified you may rely on keys, values and each to repeatedly return the same order as each other. See "Algorithmic Complexity Attacks" in perlsec for details on why hash order is randomized. Aside from the guarantees provided here the exact details of Perl's hash algorithm and the hash traversal order are subject to change in any release of Perl.

ハッシュ要素は見かけ上、ランダムな順序で返されます。 実際のランダムな順序はハッシュに固有です; 二つのハッシュに全く同じ一連の 操作を行っても、ハッシュによって異なった順序になります。 ハッシュへの挿入によって順序が変わることがあります; 削除も同様ですが、 each または keys によって返されたもっとも 最近のキーは順序を変えることなく削除できます。 ハッシュが変更されない限り、keys, values, each が繰り返し同じ順序で返すことに依存してもかまいません。 なぜハッシュの順序がランダム化されているかの詳細については "Algorithmic Complexity Attacks" in perlsec を参照してください。 ここで保証したことを除いて、Perl のハッシュアルゴリズムとハッシュ横断順序の 正確な詳細は Perl のリリースによって変更される可能性があります。

After each has returned all entries from the hash or array, the next call to each returns the empty list in list context and undef in scalar context; the next call following that one restarts iteration. Each hash or array has its own internal iterator, accessed by each, keys, and values. The iterator is implicitly reset when each has reached the end as just described; it can be explicitly reset by calling keys or values on the hash or array, or by referencing the hash (but not array) in list context. If you add or delete a hash's elements while iterating over it, the effect on the iterator is unspecified; for example, entries may be skipped or duplicated--so don't do that. Exception: It is always safe to delete the item most recently returned by each, so the following code works properly:

each がハッシュをすべて読み込んでしまった後、 次の each 呼び出しでは、リストコンテキストでは空リストが 返され、スカラコンテキストでは undef が返されます; そのあと もう一度呼び出すと、再び反復を始めます。 ハッシュや配列毎にそれぞれ反復子があり、eachkeysvalues でアクセスされます。 反復子は、前述したように each が要素をすべて読むことによって 暗黙にリセットされます; また、ハッシュや配列に対して keys, values を呼び出すか、リストコンテキストで (配列ではなく)ハッシュを参照ことで明示的にリセットできます。 繰り返しを行なっている間に、ハッシュに要素を追加したり削除したりすると、 反復子の動作は未定義です; 例えば、要素が飛ばされたり重複したりします-- 従って、してはいけません。 例外: 一番最近に each から返されたものを削除するのは常に 安全です; これは以下のようなコードが正しく動くことを意味します:

    while (my ($key, $value) = each %hash) {
        print $key, "\n";
        delete $hash{$key};   # This is safe
    }

Tied hashes may have a different ordering behaviour to perl's hash implementation.

tie されたハッシュは、順序に関して Perl のハッシュと異なった振る舞いをします。

The iterator used by each is attached to the hash or array, and is shared between all iteration operations applied to the same hash or array. Thus all uses of each on a single hash or array advance the same iterator location. All uses of each are also subject to having the iterator reset by any use of keys or values on the same hash or array, or by the hash (but not array) being referenced in list context. This makes each-based loops quite fragile: it is easy to arrive at such a loop with the iterator already part way through the object, or to accidentally clobber the iterator state during execution of the loop body. It's easy enough to explicitly reset the iterator before starting a loop, but there is no way to insulate the iterator state used by a loop from the iterator state used by anything else that might execute during the loop body. To avoid these problems, use a foreach loop rather than while-each.

each で使われる反復子はハッシュや配列に付随し、 同じハッシュや配列に適用される全ての反復操作の間で共有されます。 従って、一つのハッシュや配列での each の全ての使用は同じ反復位置を 進めます。 また、全ての each は、同じハッシュまたはキーに対する keysvalues の使用によって、または (リストではなく)ハッシュがリストコンテキストで参照されることによって、 反復子がリセットされることになります。 これは、each を基にしたループをかなり不安定にします: そのようなループが既にオブジェクトを部分的に通りぬけた反復子で行われたり、 ループ本体の実行中に誤って反復子の状態を壊すことは容易です。 ループを始める前に反復子を明示的にリセットするのは十分容易ですが、 ループ本体の間に実行しているかもしれない他の何かによって使われている 反復子の状態から、 ループによって使われている反復子の状態を分離する方法はありません。 これらの問題を避けるには、while-each ではなく foreach ループを使ってください。

This extends to using each on the result of an anonymous hash or array constructor. A new underlying array or hash is created each time so each will always start iterating from scratch, eg:

これは、無名ハッシュや配列のコンストラクタの結果に each を使うように拡張します。 新しい基となる配列やハッシュは毎回作られるので、 それぞれは常に最初から反復します; 例:

  # loops forever
  while (my ($key, $value) = each @{ +{ a => 1 } }) {
      print "$key=$value\n";
  }

This prints out your environment like the printenv(1) program, but in a different order:

これは、printenv(1) プログラムのように環境変数を表示しますが、 順序は異なっています:

    while (my ($key,$value) = each %ENV) {
        print "$key=$value\n";
    }

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

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

As of Perl 5.18 you can use a bare each in a while loop, which will set $_ on every iteration. If either an each expression or an explicit assignment of an each expression to a scalar is used as a while/for condition, then the condition actually tests for definedness of the expression's value, not for its regular truth value.

Perl 5.18 から while ループの中に裸の each を書けます; これは繰り返し毎に $_ を設定します。 each 式または each 式からスカラへの明示的な代入が while/for の条件部として使われた場合、 条件は通常の真の値かどうかではなく、式の値が定義されているかどうかを テストします。

    while (each %ENV) {
        print "$_=$ENV{$_}\n";
    }

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

あなたのコードを以前のバージョンの Perl で実行したユーザーが不思議な 文法エラーで混乱することを避けるために、コードが最近のバージョンの Perl で のみ 動作することを示すためにファイルの先頭に以下のようなことを 書いてください:

    use v5.12;  # so keys/values/each work on arrays
    use v5.18;  # so each assigns to $_ in a lone while test

See also keys, values, and sort.

keysvaluessort も参照してください。