perlfunc gods <P> keys - retrieve list of indices from a hash <P> <HR> <P> keys <FONT SIZE=-1>HASH</FONT> <P> <HR> <P> Returns a list consisting of all the keys of the named hash. (In a scalar context, returns the number of keys.) The keys are returned in an apparently random order, but it is the same order as either the [perlfunc:values|values()] or [perlfunc:each|each()] function produces (given that the hash has not been modified). As a side effect, it resets HASH's iterator. <P> Here is yet another way to print your environment: <P> <PRE> @keys = keys %ENV; @values = values %ENV; while ($#keys &gt;= 0) { print pop(@keys), '=', pop(@values), &quot;\n&quot;; } </PRE> <P> or how about sorted by key: <P> <PRE> foreach $key (sort(keys %ENV)) { print $key, '=', $ENV{$key}, &quot;\n&quot;; } </PRE> <P> To sort an array by value, you'll need to use a [perlfunc:sort|sort()] function. Here's a descending numeric sort of a hash by its values: <P> <PRE> foreach $key (sort { $hash{$b} &lt;=&gt; $hash{$a} } keys %hash) { printf &quot;%4d %s\n&quot;, $hash{$key}, $key; } </PRE> <P> As an lvalue [perlfunc:keys|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 <P> <PRE> keys %hash = 200; </PRE> <P> then <CODE>%hash</CODE> 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 <CODE>%hash = ()</CODE>, use <CODE>undef %hash</CODE> if you want to free the storage while <CODE>%hash</CODE> is still in scope. You can't shrink the number of buckets allocated for the hash using [perlfunc:keys|keys()] in this way (but you needn't worry about doing this by accident, as trying has no effect). <HR>