perl-5.38.0
keys HASH
keys ARRAY

Called in list context, returns a list consisting of all the keys of the named hash, or in Perl 5.12 or later only, the indices of an array. Perl releases prior to 5.12 will produce a syntax error if you try to use an array argument. In scalar context, returns the number of keys or indices.

リストコンテキストで呼び出されると、指定したハッシュのすべてのキー、あるいは Perl 5.12 以降でのみ、配列のインデックスからなるリストを返します。 5.12 より前の 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. Tied hashes may behave differently to Perl's hashes with respect to changes in order on insertion and deletion of items.

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

As a side effect, calling keys resets the internal iterator of the HASH or ARRAY (see each) before yielding the keys. In particular, calling keys in void context resets the iterator with no other overhead.

副作用として、keys の呼び出しは、 キーを取り出す前に HASH や ARRAY の反復子を 初期化します (each を参照してください)。 特に、無効コンテキストで keys を呼び出すと オーバーヘッドなしで反復子を初期化します。

Here is yet another way to print your environment:

環境変数を表示する別の例です:

    my @keys = keys %ENV;
    my @values = values %ENV;
    while (@keys) {
        print pop(@keys), '=', pop(@values), "\n";
    }

or how about sorted by key:

key でソートしてもいいでしょう:

    foreach my $key (sort(keys %ENV)) {
        print $key, '=', $ENV{$key}, "\n";
    }

The returned values are copies of the original keys in the hash, so modifying them will not affect the original hash. Compare values.

返される値はハッシュにある元のキーのコピーなので、 これを変更しても元のハッシュには影響を与えません。 values と比較してください。

To sort a hash by value, you'll need to use a sort function. Here's a descending numeric sort of a hash by its values:

ハッシュを値でソートするためには、sort 関数を使う 必要があります。 以下ではハッシュの値を数値の降順でソートしています:

    foreach my $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
        printf "%4d %s\n", $hash{$key}, $key;
    }

Used as an lvalue, keys allows you to increase the number of hash buckets allocated for the given hash. This can gain you a measure of efficiency if you know the hash is going to get big. (This is similar to pre-extending an array by assigning a larger number to $#array.) If you say

左辺値として使うことで、keys を使うことで与えられたハッシュに 割り当てられたハッシュ表の大きさを増やすことができます。 これによって、ハッシュが大きくなっていくなっていくときの 効率の測定ができます。 (これは大きい値を $#array に代入することで配列を予め拡張することに 似ています。) 以下のようにすると:

    keys %hash = 200;

then %hash will have at least 200 buckets allocated for it--256 of them, in fact, since it rounds up to the next power of two. These buckets will be retained even if you do %hash = (), use undef %hash if you want to free the storage while %hash is still in scope. You can't shrink the number of buckets allocated for the hash using keys in this way (but you needn't worry about doing this by accident, as trying has no effect). keys @array in an lvalue context is a syntax error.

%hash は少なくとも 200 の大きさの表が割り当てられます -- 実際には 2 のべき乗に切り上げられるので、256 が割り当てられます。 この表はたとえ %hash = () としても残るので、 もし %hash がスコープにいるうちにこの領域を開放したい場合は undef %hash を使います。 この方法で keys を使うことで、表の大きさを小さくすることは できません (間違えてそのようなことをしても何も起きないので気にすることはありません)。 左辺値コンテキストでの keys @array は文法エラーとなります。

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

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

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

See also each, values, and sort.

each, values, sort も参照してください。