http://www.perlmonks.org?node_id=929615


in reply to Re: Determine largest key in hash
in thread Determine largest key in hash

Hi.

Thank you for the response. I can give an overview of what I'm trying to do.

Pretend have a loop
my $cnt = 0; while(1) { $cnt++; #print event trigger from hash }
At every iteration I need to check to see if I have an "event trigger" in the hash. If I do, I need to print it. I can't do a simple hash because each iteration (of the counter) COULD have more than one event trigger.
my %hash = ( 1 => { 1 => "nothing", name => "Shrek", event => "I saw shrek!", }, 6 => { 1 => "nothing", name => "Donkey", event => "I saw the donkey!", 2 => "nothing", name => "Fiona", event => "I saw Fiona!" }, );
So on our first iteration we'd get triggered that we found Shrek. Sweet! It works well because we only had the one event.

However for the 6th time through the loop we have TWO events. That is why I need to numerically index them because I never know.. maybe an event happens in my code and I need to create a third or fourth event for iteration 6 and I need them ALL to work properly.

So the numerics are only for use in determining which iteration to come out on, and then they are used to allow me to use as many events as necessary (the event names in the real code are NOT unique so I would not be able to use the names of them. Numbers were the only thing I could think of to make this work.

Replies are listed 'Best First'.
Re^3: Determine largest key in hash
by MidLifeXis (Monsignor) on Oct 04, 2011 at 17:56 UTC

    Ok, Given that use, I would probably do either a sparse array, or a *cringe* hash with numeric keys. Please note that your data under each first level key is not correct...

    my %hash = ( 1 => [ { name => "Shrek", event => "I saw shrek!" }, ], 6 => [ { name => "Donkey", event => "I saw the donkey!" }, { name => "Fiona", event => "I saw Fiona!" }, ], );

    You would access this stuff then with....

    for my $trigger ( @{ $hash{$cnt} || [] } ) { # Do work here with $trigger .... }

    See Re: Determine largest key in hash for details on how to push data into the trigger list.

    As a sparse array, this would look like...

    my @data = ( [ # 0 (remember, we are zero-based) { name => "Shrek", event => "I saw shrek!" }, ], undef, # 1 undef, # 2 undef, # 3 undef, # 4 [ # 5 - zero-based => 6th element { name => "Donkey", event => "I saw the donkey!" }, { name => "Fiona", event => "I saw Fiona!" }, ], );

    You would change your loop to...

    for my $trigger ( @{ $data[$cnt-1] || [] } ) { # Do work here with $trigger .... }

    I still have a little bit of a cringe-factor with the layout of the sparse array, and would probably reevaluate the control logic around that, but this shows a couple of ways where the structure can make quite a difference in how you manage your data.

    --MidLifeXis

Re^3: Determine largest key in hash
by BrowserUk (Patriarch) on Oct 04, 2011 at 18:02 UTC

    That will not work. You build a hash like this:

    [0] Perl> %hash = ( 1 => { 1 => "nothing", name => "Shrek", event => "I saw shrek!", }, 6 => { 1 => "nothing", name => "Donkey", event => "I saw the donkey!", 2 => "nothing", name => "Fiona", event => "I saw Fiona!" }, );;

    But end up with this:

    pp \%hash;; { 1 => { 1 => "nothing", event => "I saw shrek!", name => "Shrek" }, 6 => { 1 => "nothing", 2 => "nothing", event => "I saw Fiona!", name + => "Fiona" }, }

    NOTE: that in the hash key by '6' there is only one 'name', and only one 'event'.

    You cannot have duplicate keys in a hash. You need to rethink your data structure.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.