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...
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.