NathanE has asked for the wisdom of the Perl Monks concerning the following question:

Hey all, once again;

I'm using a hash of a hash of a hash to store some numeric information, and using the actual hash key as a value to be retrieved to be associated with that numeric value for print out. It all representing a score that allows my program to include or exclude that key value to be/or not to be printed out.

When my program decides to save the key and change increment the number, another value is also incremented added to a different hash of a hash within the same base hash a la;

$Stats{$DestPort}++; $Stats{$DestPort}{covert}{"$intSrcFound|$hshIPtoDomainIndex{$strDst}|$ +DstIP|$SrcIP|$DestPort|$Protocol"}++;
But, when I try to retrieve the values, I end up with keys that contain a different $DestPort than the $DestPort I have specified for $Stats{$DestPort}.

So, my program obtains a list @TopPorts, and I iterate through this list, during which time I sort the hash by it's keys and do the following:

foreach $entry (sort keys %{ $Stats{$TopPorts[$a]}{covert} }) { # my code to print out various HTML # elements is typically here }
Which is where I run into the aforementioned problem of $entry containing an element that should corrospond to $TopPort$a being completely off. Is there an issue with hashes of hashes contaminating each other, or is there some small property I'm unaware of here?

Thanks for everyone's help!

Update <FIXED>: Error occured due to reference conflict with integer/string. Thanks everyone.

Replies are listed 'Best First'.
Re: hash of hash - incorrect values?
by friedo (Prior) on Aug 03, 2005 at 19:30 UTC
    $Stats{$DestPort}++; $Stats{$DestPort}{covert}{$intSrcFound| $hshIPtoDomainIndex{$strDst}|$ +DstIP|$SrcIP|$DestPort|$Protocol"}++;

    This doesn't make much sense. $Stats{$DestPort} can only be one thing at a time. In the first line you are incrementing a presumably numeric value, in the next line you are assigning a reference to a deep nested structure.

      I guess that's part of my question. Can a lower level of a hash contain a value and also be linked to other levels of hashes? I thought they could, but maybe not?

      If they can't, than I don't understand how I am obtaining values at all, both for the incremented integer and also for the key and the related value in the deeply nested structure.

      comments?

        You misunderstand $hash{a}{b}. Hashes only have one level. $hash{a}{b} is a hash of a hash. $hash{a}{b} is a shortcut for ${$hash{a}}{b}. That means "the value at key 'b' in the hash referenced by $hash{a}." $hash{a} can't contain both a hash reference and a number, so $hash{a}++ and $hash{a}{b} conflict.

        There is an easy solution. Replace

        $Stats{$DestPort}++; $Stats{$DestPort}{covert}{"..."}++;

        with

        $Stats{$DestPort}{total}++; $Stats{$DestPort}{covert}{"..."}++;

        or

        $Stats{$DestPort}{total}++; $Stats{$DestPort}{item}{covert}{"..."}++;