Hi all. Sorry for yet another tree question. I've been trying to understand all the various examples you have already sent me.

What I have been trying to do this morning is understand how the Tree::Simple module works specifically. I want to take an input file that contains a delimited file of child/parents, build a tree, and output a flattened version.

If I had:

apple:fruit granny smith:apple rotten granny smith:granny smith . . . orange|fruit
the output would be:

Fruit:Apple:Granny Smith:Rotten Granny Smith Fruit:Orange

The example on the module page for Tree::Simple is helpful, but I am trying to figure out how to read in my input file. I am assuming I'd use a while loop. Any tips on tweaking the below code?

use warnings; use strict; use Tree::Simple; # make a tree root my $tree = Tree::Simple->new("0", Tree::Simple->ROOT); # explicity add a child to it $tree->addChild(Tree::Simple->new("1")); # specify the parent when creating # an instance and it adds the child implicity my $sub_tree = Tree::Simple->new("2", $tree); # chain method calls $tree->getChild(0)->addChild(Tree::Simple->new("1.1")); # add more than one child at a time $sub_tree->addChildren( Tree::Simple->new("2.1"), Tree::Simple->new("2.2") ); # add siblings $sub_tree->addSibling(Tree::Simple->new("3")); # insert children a specified index $sub_tree->insertChild(1, Tree::Simple->new("2.1a")); # clean up circular references $tree->DESTROY(); $tree->traverse(sub { my ($_tree) = @_; print (("\t" x $_tree->getDepth()), $_tree->getNodeValue(), "\n"); });

Here is what I have so far:

use warnings; use strict; use Tree::Simple; my $tree = Tree::Simple->new("0", Tree::Simple->ROOT); while(<DATA>) { chomp; my ($child, $parent) = split ":"; my $sub_tree = Tree::Simple->new("$parent", $tree); $sub_tree->addChild(Tree::Simple->new("$child")); $tree->DESTROY(); } $tree->traverse(sub { my ($_tree) = @_; print (("\t" x $_tree->getDepth()), $_tree->getNodeValue(), "\n"); }); __DATA__ apple:fruit granny smith:apple rotten granny smith:granny smith orange:fruit
Output:

fruit apple apple granny smith granny smith rotten granny smith fruit orange

In reply to Using Tree::Simple with while loop and input file. by bryank

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.