Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tree Traversal w/ perl

Status
Not open for further replies.

133tcamel

Programmer
Sep 22, 2002
137
IN
Hi,
I have this hash which is like a tree.. the hash key is a number and the value is a array of numbers.. these array values are also keys in the same hash.. e.g.
$hash{'1'} = ARR('3', '5', '9');
$hash{'3'} = ARR('2', '7');
$hash{'2'} = ARR();
$hash{'5'} = ARR('10', '11');
and so on...
what I need is a graphical representation so that I can start with "$hash{'1'}" and traverse each branch and print a corresponding output..

Code:
  1
   |-3
   |  |- 2
   |-5
      |- 10 
 ...

I guess I can use a brute force method of using many many for loops, but I'm looking for an easy to understand efficient method to do this.

any help will be appreciated.

---
cheers!
san.
 
what you want is actually very easy because your data structure is very simple
foreach my $key(sort keys %hash){
print OUT "$key @{$hash{$key}}\n";# you can format @$hash{$key} as you wish using join for example
}
Is this too messy, then how about

require 'dumpvar.pl';

...
&dumpValue(\%hash);

Oops, I guess you want a 'link' analysis. But are these unique?

svar


 
thanks for the reply man, but I want more like a DOS "tree" command type out (like in my first post)
literally speaking and using the example from my first post, the program should first traverse $hash{'1'} and then $hash{'3'} followed by hash{'2'}. Since there are no further keys after hash{'2'}, so it should backtrack to $hash{'1'} and proceed with the next element, i.e. traversing $hash{'5'}

---
cheers!
san.
 
what you want is actually very easy because your data structure is very simple
foreach my $key(sort keys %hash){
print OUT "$key @{$hash{$key}}\n";# you can format @$hash{$key} as you wish using join for example
}
Is this too messy, then how about

require 'dumpvar.pl';

...
&dumpValue(\%hash);

Oops, I guess you want a 'link' analysis. But are these unique?

svar


 
Have a look at this:

use Data::Dumper;
# simple procedural interface
print Dumper($foo, $bar);
# extended usage with names
print Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]);
Mike

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
thanks very much to all those who replied.. I finally found the answer in the perl cookbook after a lot of searching

just for reference, this is what I found

Code:
sub output {
    my($root, $prefix, $width) = (shift, shift || '', shift || 0);
    my $path = $root;
    my $size = $Dirsize{$root};
    my $line = sprintf("%${width}d %s", $size, $path);
    print $prefix, $line, "\n";
    for ($prefix .= $line) {        # build up more output
        s/\d /| /;
        s/[^|]/ /g;
    }
    if ($Kids{$root}) {             # not a bachelor node
        my @Kids = @{ $Kids{$root} };
            $Dirsize{$Kids[0]} =~ /(\d+)/;
        my $width = length $1;
        for my $kid (@Kids) { output($kid, $prefix, $width) }
    }
}

Thanks for your replies too and its much appreciated :) ---
cheers!
san.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top