Dru has asked for the wisdom of the Perl Monks concerning the following question:
Esteemed Monks,
I am a big fan of this data structure, but I am ashamed to admit I am not even sure what it is called. I know it is some form of hash (obviously), but I have been pouring over the perldocs, especially perldsc, to try and finally figure out it's name, but I have finally admitted defeat. It appears to be a simple hash, but the key linking it to {$ip} is what is confusing me.
I would also like to know if it is possible to add a new key/value pair to the existing hash without having to create a new one, which is what I did to get the results I was after.
This is my existing data structure:
I am a big fan of this data structure, but I am ashamed to admit I am not even sure what it is called. I know it is some form of hash (obviously), but I have been pouring over the perldocs, especially perldsc, to try and finally figure out it's name, but I have finally admitted defeat. It appears to be a simple hash, but the key linking it to {$ip} is what is confusing me.
I would also like to know if it is possible to add a new key/value pair to the existing hash without having to create a new one, which is what I did to get the results I was after.
This is my existing data structure:
and later in my code, I would like to add:$hashIPs{$ip} = { totalVulns => $ipTotalVulns, vulns => $vulns };
I was able to accomplish what I wanted with the following, but for curiosity's sake, I would like to know if I could have avoided having to create the 2nd hash.region => $region
my $hashIPs{$ip} = { totalVulns => $ipTotalVulns, vulns => $vulns }; my $hashrefIPs = \%hashIPs; my $hashIPsNew{$ip} = { region => $region, totalVulns => ${$hashrefIPs}{$ip}{totalVulns}, vulns => ${$hashrefIPs}{$ip}{vulns} }; for my $ip (keys %hashIPsNew){ print "$ip -- $hashIPsNew{$ip}{region} -- $hashIPsNew{$ip}{totalVulns} + -- $hashIPsNew{$ip}{vulns}\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Mystery (to me) Data Structure
by moritz (Cardinal) on Mar 31, 2008 at 10:53 UTC | |
by Dru (Hermit) on Mar 31, 2008 at 14:24 UTC | |
Re: Mystery (to me) Data Structure
by poolpi (Hermit) on Mar 31, 2008 at 10:42 UTC | |
by jfraire (Beadle) on Apr 01, 2008 at 04:29 UTC |
Back to
Seekers of Perl Wisdom