OP's concern was not the use of keys, but the potential cost of using it due to a perceived 'temporary array'. However various solutions to that problem have been suggested. Let's see how yours stacks up:

use strict; use warnings; use Benchmark qw(cmpthese); my %bigHash = map {$_ => undef} 1 .. 1e3; my ($key) = each %bigHash; my $count = keys %bigHash; my ($key2) = (%bigHash)[0]; print "First key '$key', second '$key2'\n"; cmpthese ( -1, { byEach => sub {scalar keys %bigHash; my ($key) = each %bigHas +h;}, byArray => sub {my $key = (%bigHash)[0]}, } );

Prints:

First key '559', second '559' Rate byArray byEach byArray 5228/s -- -100% byEach 2414187/s 46082% --

It's that 'temporary array' OP was worried about. A fancy solution no doubt but, for saving time, not a good one.


True laziness is hard work

In reply to Re^2: Pick any key from a very large hash by GrandFather
in thread Pick any key from a very large hash by FloydATC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.