Hello, fellow monks!
I have to represent a dependency graph before inserting a new attribute value into an LDAP server. It's actually that objectClasses have a kind of IS-A dependencies... I thought that a hash of hashes of hashes of... could do the trick, but I am having a serious problem.
It's shown in the following code:
# small example my $tree = { top => { inetUser => {}, person => { organizationalPerson => { managedPerson => { personnel => {}, }, }, }, application => { managedApplication => {}, }, }, }; # I need to get the nodes that lead to the one I am looking for sub explore { my $node = shift; my $target = shift; my $stack = shift; # we found it, so we may leave... return $stack if $target eq $stack->[-1]; # we keep looking... my $kids = scalar keys %$node; if ( $kids > 0 ) { for my $key ( keys %$node ) { push @$stack, $key; show( $stack ); explore( $node->{$key}, $target, $stack, $OK ); pop @$stack; } } else { # leaf... no-op } } sub show { print "$_ " for @{$_[0]}; print "\n" } # test my $stack; eval { explore( $tree, 'personnel', [] ); # expected result: "top person organizationalPerson managedPerson +personnel "; }; $stack = $@; show( $stack ); __END__
This works OK, but it's rather an ugly hack... Any way to do this without the exception? thanks!
--
our $Perl6 is Fantastic;
In reply to trees and depth-first search by Excalibor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |