http://qs321.pair.com?node_id=771244


in reply to Re: how to construct tree from parent pointer list
in thread how to construct tree from parent pointer list

Hi Perl monks.

I am in search of wisdom!

I was wondering if you could explain a couple things for me from your example:

$nodes{$_}{name}||=$_ for $c,$p;

-- I'm not use to putting '$_' as a key for a hash. I am also not sure what '||=$_' means/does. Can you explain?

I'm also not sure why you are deleting children...

delete $nodes{$_} for @children;

Sorry for the newbie questions -- I've been dabbling in Perl for years, and I use hashes a lot but my syntax is still on the simple side.

I have another question. Any tips on printing nested hashes? I use Data::Dumper all the time, but I usually just use the default output. What I am looking for is the "full path". For example, in the above example, we have this:

'name' => 'a', 'kids' => { 'c' => { 'name' => 'c', 'kids' => { 'e' => { 'name' => 'e' }, 'f' => { 'name' => 'f'

-- I'd like to print out something like this:

a a:c a:c:e a:c:f

I usually just print each piece of the hash, which isn't too hard if I created the hash. For example, if I had a hash ref that had name/address information, I'd do something like:

print $hash->{$key}->{'name'} . "\t"; print $hash->{$key}->{'addres'} . "\n";

But if the hash is created via autovivification (for example, let's say I am constructing a complex node tree from a parent-child list of several thousand nodes) I get stumped on how I'd be able to print the full path of each element in the hash.

Does that make any sense? I am waking up and keeping two little kids from killing each other. ;)

Any tips much appreciated!!

Thanks.

--Bryan