Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Mystery (to me) Data Structure

by Dru (Hermit)
on Mar 31, 2008 at 10:21 UTC ( #677494=perlquestion: print w/ replies, xml ) Need Help??
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:
$hashIPs{$ip} = { totalVulns => $ipTotalVulns, vulns => $vulns };
and later in my code, I would like to add:
region => $region
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.
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"; }
Thanks,
Dru

Perl, the Leatherman of Programming languages. - qazwart

Comment on Mystery (to me) Data Structure
Select or Download Code
Re: Mystery (to me) Data Structure
by poolpi (Hermit) on Mar 31, 2008 at 10:42 UTC
    my $hashIPs = {}; $hashIPs->{$ip} = { totalVulns => $ipTotalVulns, vulns => $vulns }; + $hashIPs->{$ip}->{'region'} = $region;

    hth,
    PooLpi

    Update:

    Or without references : my %hashIPs; $hashIPs{$ip} = { totalVulns => $ipTotalVulns, vulns => $vulns }; $hashIPs{$ip}{'region'} = $region;
    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

      Clarification: $hashIPs{$ip}{'region'} = $region; is still using references. It's just that the arrow operator is optional between bracket subscripts, according to perlref.

      Julio

      Update: And this is really nice when working with arrays of arrays, since they are then used as multidimensional arrays: $array_ref->[$x][$y] or $array[$x][$y].

Re: Mystery (to me) Data Structure
by moritz (Cardinal) on Mar 31, 2008 at 10:53 UTC
    but I am ashamed to admit I am not even sure what it is called

    In this case it's a hash of hashes, or just "nested hashes".

    But when data structures grow more complicated it doesn't make sense to invent ever more complicated names for them. Usually a Data::Dumper output of an example data structure explains its structure.

      In this case it's a hash of hashes, or just "nested hashes".

      Thanks, a HoH is what I thought it was, but it didn't quite resemble the the HoH definition in perldsc so it threw me for a loop, but now I realize they are initializing the hash while I am adding elements to it.

      Thanks,
      Dru

      Perl, the Leatherman of Programming languages. - qazwart

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://677494]
Approved by andreas1234567
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (16)
As of 2014-12-18 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (58 votes), past polls