Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Test whether a hash key is presentReturn a list of the values in ... (Berikutnya)
Functions for real %HASHes

Retrieve list of indices from a hash

Daftar Isi

  • keys HASH

  • keys ARRAY
  • keys EXPR

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

    The keys of a hash are returned in an apparently random order. The actualrandom order is subject to change in future versions of Perl, but itis guaranteed to be the same order as either the values or eachfunction produces (given that the hash has not been modified). SincePerl 5.8.1 the ordering can be different even between different runs ofPerl for security reasons (see Algorithmic Complexity Attacks in perlsec).

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

    Here is yet another way to print your environment:

    1. @keys = keys %ENV;
    2. @values = values %ENV;
    3. while (@keys) {
    4. print pop(@keys), '=', pop(@values), "\n";
    5. }

    or how about sorted by key:

    1. foreach $key (sort(keys %ENV)) {
    2. print $key, '=', $ENV{$key}, "\n";
    3. }

    The returned values are copies of the original keys in the hash, somodifying them will not affect the original hash. Compare 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:

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

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

    1. 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. Thesebuckets 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 usingkeys 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 syntaxerror.

    Starting with Perl 5.14, keys can take a scalar EXPR, which must containa reference to an unblessed hash or array. The argument will bedereferenced automatically. This aspect of keys is considered highlyexperimental. The exact behaviour may change in a future version of Perl.

    1. for (keys $hashref) { ... }
    2. for (keys $obj->get_arrayref) { ... }

    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.012;# so keys/values/each work on arrays
    2. use 5.014;# so keys/values/each work on scalars (experimental)

    See also each, values, and sort.

 
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) Test whether a hash key is presentReturn a list of the values in ... (Berikutnya)