CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced: Re: [sriharsha_12] Tree Hierarchy: Edit Log



Zhris
Enthusiast

Oct 6, 2011, 1:23 PM


Views: 3747
Re: [sriharsha_12] Tree Hierarchy

Hi,

Where is the tree structure derived? You mention you generated a hash, is this meant to be the hard coded tree? Otherwise, inevitably the first task is to parse the tree structure into some kind of usable Perl data structure. Maybe provide some more information.

(Update: I wrote a very rough script (not great) to demonstate some fundamental aspects i.e. recursive subroutine etc):


Code
######################################## 

#! /usr/bin/perl
use strict;
use warnings;

########################################

my $tree = {
a => {
b => {
d => { },
e => { }
},
c => {
b => {
d => { },
e => { }
}
}
}
};

my $input = 'e';

########################################

my $data = process($tree);

foreach (keys %$data) {
print reverse($_) . "\n" if ($_ =~ /$input$/);
}

########################################

sub process
{
my $ref = shift || die "Tree required.\n";
my $key_string = shift || '';
my $data = shift || { };

foreach my $key (keys %$ref)
{
my $next_ref = $ref->{$key};
my $next_key_string = $key_string . ',' . $key;
$data->{$next_key_string} = undef;

process($next_ref, $next_key_string, $data);
}

return $data;
}

########################################


Chris


(This post was edited by Zhris on Oct 6, 2011, 2:27 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Oct 6, 2011, 2:14 PM
Post edited by Zhris (Enthusiast) on Oct 6, 2011, 2:17 PM
Post edited by Zhris (Enthusiast) on Oct 6, 2011, 2:27 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives