
Zhris
Enthusiast
Oct 6, 2011, 1:23 PM
Post #2 of 5
(6103 views)
|
Re: [sriharsha_12] Tree Hierarchy
[In reply to]
|
Can't Post
|
|
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):
######################################## #! /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)
|