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

Zhris
User
Oct 6, 2011, 1:23 PM
Views: 3170
|
|
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):
######################################## #! /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
(User) on Oct 6, 2011, 2:14 PM
|
|
Post edited by Zhris
(User) on Oct 6, 2011, 2:17 PM
|
|
Post edited by Zhris
(User) on Oct 6, 2011, 2:27 PM
|
|
|  |