I have a very small image viewer script which has a database full of id numbers from 1 - infinity. I delete a few of them from the database and my script knows how to ++ or -- to the next eligible entry in case there's a gap in ids.

I wrote the code quite a few years ago and now I'm thinking there's GOT to be a better way to do this. Here's a sample of my script finding the next available image

# determine next eligible image my $next_cnt = 0; while ($lowest) { $next_cnt++; if (exists $upload{$next_cnt}) { last; } } $next_cnt = $next_cnt + 2; # jump one ahead
I want to know if there's a way to determine what index a certain number is, inside my hash. Now I know indexes are used with arrays, but is there a similar way to do this in hashes if the keys are numbers?

Ie:

1,2,3,5,9,10
We are on image 5 and want to find out what the previous and next number in sequence would be..
my $prev = $db{$picid}..
There just has to be a way to automatically determine, in a sorted hash, what the previous and next items would be. Right?

In reply to how to check which index $x is in an hash by Anonymous Monk

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.