Assuming you know the first key, you have a few simple options. The first first is to pull the keys from the nested hash and do a search through them.

my $max_event; map {$max_event = $_ if $_ > $max_event} keys %{$hash{1}};

You can then use $max_event to determine what your next event should be.

If you have to do this many times, I would suggest an additional field in your hash that tracks the max event like so:

%hash = ( 1 => { 1 => "", #dynamically created name => "", that => "", current_max => 1, }, 2.... # iteration );

Now if you need to know the current highest event, you just examine $hash{1}{current_max}. You'll have to ensure that you increment this field each time you add an event.

This approach could be a problem if the event could have a valid data field named current_max. In this case, you will have to modify your data structure to ensure that the current_max field is cleanly separated from the event data. But it would otherwise work the same.


In reply to Re: Determine largest key in hash by tj_thompson
in thread Determine largest key in 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.