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

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

I have a hash that I populate by extracting data from an xml file.
At some point in my script the following assignment is made
$children{$parent_id}{$child_id}{'id'} = $newid;

Just a note that every $parentid has many children. Now I want to get just the values of each child for a certain parent. I thought that the code below would work, but it does not:
@{ $children{$parent_id} });

neither does this:
keys($children{$parent_id})

How do I get the key values for the first branch (parent id) of the hash? I hope my question makes sense.
Thank you

Replies are listed 'Best First'.
Re: Trying to pull the key values from a Hash
by bellaire (Hermit) on Nov 24, 2009 at 15:20 UTC
    Beneath the top level, your autovivified hashes are actually hash references. Try:
    keys %{$children{$parent_id}};
Re: Trying to pull the key values from a Hash
by BioLion (Curate) on Nov 24, 2009 at 15:53 UTC

    As well as what bellaire says you might also want to check perldoc for the corect usage of keys and values. The tutorials in perldoc are also really useful - they have good pages on use of complex data structures (like your hash of hashes) and references:

  • perldsc
  • perlref
  • Good luck and remember that practice makes perfect and if it doesn't there is always PerlMonks!

    Just a something something...
      Good luck and remember that practice makes perfect and if it doesn't there is always PerlMonks!
      I think that this is my new favourite saying. I suggested it over in the monkquips thread, but I'm not sure if anyone is still reading there.

        *blush*!

        Just a something something...
Re: Trying to pull the key values from a Hash
by planetscape (Chancellor) on Nov 24, 2009 at 20:34 UTC
Re: Trying to pull the key values from a Hash
by biohisham (Priest) on Nov 24, 2009 at 16:14 UTC
    "I want to get just the values of each child for a certain parent"

    "How do I get the key values for the first branch (parent id) of the hash?"

    Note that the values for each key for parents is actually a reference and so is the value for each child key(a, b, c) in this data structure... If you meant by values that you want the IDs for each child of a certain parent, as I understood, then I hope the following excerpt of code can be of help.
    #!/usr/local/bin/perl use strict; use warnings; my %children; #each parent has 3 children identified by their IDs $children{'parent1'}{'a'}{'id'} = 'Child1P1'; $children{'parent1'}{'b'}{'id'} = 'Child2P1'; $children{'parent1'}{'c'}{'id'} = 'Child3P1'; $children{'parent2'}{'a'}{'id'} = 'Child1P2'; $children{'parent2'}{'b'}{'id'} = 'Child2P2'; $children{'parent2'}{'c'}{'id'} = 'Child3P2'; for my $parent (sort keys (%children)){ print "The key value is a reference of type: ", ref $children{$par +ent}, $/; print "The parent is $parent:\n"; for my $child (keys (%{$children{$parent}})){ print "children of $parent-> $children{$parent}{$child}{'id +'}\n"; } print "\n\n"; }
    #OUTPUT:
    The key value is a reference of type: HASH The parent is parent1: children of parent1-> Child3P1 children of parent1-> Child1P1 children of parent1-> Child2P1 The key value is a reference of type: HASH The parent is parent2: children of parent2-> Child3P2 children of parent2-> Child1P2 children of parent2-> Child2P2


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.