Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found