I have a very large (DB_File) hash which may have millions of keys in it, and I want to pick just one of them. Note that I do NOT want a truly random key, this is not easy way to get random key from hash?. I just want a key, exactly which one is not important. (The hash contains a pool of resources which may be added and removed arbitrarily)

I tried experimenting with my ($key,$value) = each %hash but I'm getting... weird results. I'm thinking this is probably because each is meant for looping through the entire hash while I'm only calling it now and then, in between adding and deleting key/value pairs.

Using keys or splice()is out of the question, the temporary array would just kill me. Any bright ideas? Is there perhaps a way to "reset" each after an incomplete scan?

-- Time flies when you don't know what you're doing

In reply to 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.