in reply to Determine largest key in hash

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.

Replies are listed 'Best First'.
Re^2: Determine largest key in hash
by Anonymous Monk on Oct 04, 2011 at 18:42 UTC
    Clever and simple. I didn't think about going at it this way.. I think that will work perfectly. Thank you!