Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Use of Tree::Parser

by Generoso (Prior)
on May 25, 2010 at 17:46 UTC ( [id://841607]=perlquestion: print w/replies, xml ) Need Help??

Generoso has asked for the wisdom of the Perl Monks concerning the following question:

Hi Dear Monks,

I am tring to parse a tree with tree::parser,

the data is as follows:

List of hash table: 16098 => 16078 => 16101 16015 => 16022, 16033, 16047, 16074, 16090 16029 => 16003 16097 => 16035 16057 => 16052 => 16001 => 16015, 16023, 16026, 16027, 16058, 16110 16101 => 16089 16007 => 16104 => 16020, 16059, 16069, 16078, 16088 16014 => 16024, 16046, 16098 16105 => 16000 => 16066, 16116 16087 => 16085 16074 => 16002, 16043, 16109 16053 => 16051, 16092, 16099 16037 => 16041, 16102 16036 => 16056 => 16005, 16112 16093 => 16075, 16083 16111 => 16016, 16025, 16039, 16053, 16091 16100 => 16019, 16073, 16107 16042 => 16035 => 16082 => 16031 16004 => 16114 => 16081 16033 => 16106 16010 => 16072 => 16064 16084 => 16088 => 16027 => 16097 16116 => 16007, 16095 16113 => 16110 => 16050, 16056, 16067, 16082, 16111 16090 => 16008, 16114 16026 => 16005 => 16024 => 16080 => 16054 16003 => 16051 => 16025 => 16054 => 16002 => 16060 16916 => 16076 16070 => 16102 => 16063 16044 => 16060 => 16063 => 16089 => 16062 => 16055 => 16052 16075 => 16069 => 16113 16040 => 16030 16034 => 16017, 16018, 16049 16012 => 16064 => 16043 => 16086 16013 => 16085 => 16079 => 16099 => 16067 => 16077 => 16108 16032 => 16062 16066 => 16001, 16014, 16037, 16072, 16093, 16104 16045 => 16107 => 16029, 16080 16011 => 16012 16059 => 16112 => 16106 => 16079 16094 => 16028 16083 => 16038, 16042, 16045, 16105 16049 => 16004, 16013 16092 => 16061 => 16010 16030 => 16038 => 16031 => 16039 => 16047 => 16077 16021 => 16016 => 16081 => 16020 => 16009, 16048 16009 => 16041 => 16073 => 16040 16095 => 16017 => 16916, 16103 16108 => 16050 => 16023 => 16034, 16055, 16100 16076 => 16006 => 16058 => 16032, 16071, 16094 16086 => 16091 => 16006, 16057 16019 => 16084 16046 => 16028 => 16044 16008 => 16087, 16096 16071 => 16070 16103 => 16109 => 16061 16018 => 16011, 16068 16048 => 16096 => 16068 => 16022 => 16021, 16036
Where format is parent => children
How can I give this as input to use Tree::Parser; # create a new parser object with some input my $tp = Tree::Parser->new($input);


Tank you, you where right.
What I was really trying to do is a recursive function. my $fv = fact ('16000'); my $level = 0; my $ord = 0; sub fact { my $val = $_[0]; my $ch = $hash{$val}; my @chs = split(',',$ch); print ' 'x$level x 3,$val," ",$_[0]," ",$level," ",$ord++,"\n"; if ($ch) { $level++; foreach my $ch1(@chs){ fact($ch1); delete $hash{$ch1}; } $level--; } }

Replies are listed 'Best First'.
Re: Use of Tree::Parser
by graff (Chancellor) on May 26, 2010 at 04:36 UTC
    You haven't given us any idea of what you actually need to do with this data, and it's actually not clear to me that Tree::Parser is the right tool for this data (or for whatever you actually need to do with it). In fact, having scanned the Tree::Parser man page, my first impression is that it's not a good match for your data -- if I'm wrong about that, I'll be interested in finding out why.

    If you are starting with a flat hash (or hash of arrays) and you want to build a tree-style hash, the basic plan would be to turn it into a recursive HoH structure, such that every "leaf" node in the tree is just an empty hash ref. Something like:

    use strict; use warnings; use Data::Dumper 'Dumper'; my %tree; while (<DATA>) { chomp; next unless ( /^\s*(\d+)\s*=>\s*(.*)/ ); my ( $key, $val ) = ( $1, $2 ); if ( $val eq '' ) { # leaf node $tree{$key} = {} unless exists( $tree{$key} ); # assign an em +pty hash ref } else { for my $node ( split /, +/, $val ) { if ( ! exists( $tree{$node} )) { $tree{$node} = {}; } $tree{$key}{$node} = \$tree{$node}; push @{$tree{$node}{parent}}, $key; # keep track of node paren +ts } } } # all done: for ( sort keys %tree ) { print "======\n$_:\n", Dumper( $tree{$_} ) unless exists( $tree{$_ +}{parent} ); } # append your data below... __DATA__
    That includes a "parent" element for every node that has one or more parents, to make it easier to keep track of relations. I'm not sure if that's what you're looking for...
      What I was really trying to do is a recursive function. Thanks,
      fact ('16000'); my $level = 0; my $ord = 0; sub fact { my $val = $_[0]; my $ch = $hash{$val}; my @chs = split(',',$ch); print ' 'x$level x 3,$val," ",$_[0]," ",$level," ",$ord++,"\n"; if ($ch) { $level++; foreach my $ch1(@chs){ fact($ch1); delete $hash{$ch1}; } $level--; } }
Re: Use of Tree::Parser
by Khen1950fx (Canon) on May 26, 2010 at 06:02 UTC
    The correct way to wrap your code is </code>, not <\code>. I used this script to clean-up your code. I put your table in a text file on my desktop and used perltidy. Here's the code that I used:
    #!/usr/bin/perl use strict; use warnings; use File::Slurp; use YAML::Dumper; my $file = '/path/to/table.txt'; my $dumper = YAML::Dumper->new; $dumper->indent_width(4); print $dumper->dump( scalar read_file($file) );
    Your table:
    16098 => 16078 => 16101 16015 => 16022, 16033, 16047, 16074, 16090 16029 => 16003 16097 => 16035 16057 => 16052 => 16001 => 16015, 16023, 16026, 16027, 16058, 16110 16101 => 16089 16007 => 16104 => 16020, 16059, 16069, 16078, 16088 16014 => 16024, 16046, 16098 16105 => 16000 => 16066, 16116 16087 => 16085 16074 => 16002, 16043, 16109 16053 => 16051, 16092, 16099 16037 => 16041, 16102 16036 => 16056 => 16005, 16112 16093 => 16075, 16083 16111 => 16016, 16025, 16039, 16053, 16091 16100 => 16019, 16073, 16107 16042 => 16035 => 16082 => 16031 16004 => 16114 => 16081 16033 => 16106 16010 => 16072 => 16064 16084 => 16088 => 16027 => 16097 16116 => 16007, 16095 16113 => 16110 => 16050, 16056, 16067, 16082, 16111 16090 => 16008, 16114 16026 => 16005 => 16024 => 16080 => 16054 16003 => 16051 => 16025 => 16054 => 16002 => 16060 16916 => 16076 16070 => 16102 => 16063 16044 => 16060 => 16063 => 16089 => 16062 => 16055 => 16052 16075 => 16069 => 16113 16040 => 16030 16034 => 16017, 16018, 16049 16012 => 16064 => 16043 => 16086 16013 => 16085 => 16079 => 16099 => 16067 => 16077 => 16108 16032 => 16062 16066 => 16001, 16014, 16037, 16072, 16093, 16104 16045 => 16107 => 16029, 16080 16011 => 16012 16059 => 16112 => 16106 => 16079 16094 => 16028 16083 => 16038, 16042, 16045, 16105 16049 => 16004, 16013 16092 => 16061 => 16010 16030 => 16038 => 16031 => 16039 => 16047 => 16077 16021 => 16016 => 16081 => 16020 => 16009, 16048 16009 => 16041 => 16073 => 16040 16095 => 16017 => 16916, 16103 16108 => 16050 => 16023 => 16034, 16055, 16100 16076 => 16006 => 16058 => 16032, 16071, 16094 16086 => 16091 => 16006, 16057 16019 => 16084 16046 => 16028 => 16044 16008 => 16087, 16096 16071 => 16070 16103 => 16109 => 16061 16018 => 16011, 16068 16048 => 16096 => 16068 => 16022 => 16021, 16036

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://841607]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found