Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Hierarchical Data Structures

by gmax (Abbot)
on Mar 11, 2004 at 08:08 UTC ( #335744=note: print w/replies, xml ) Need Help??

in reply to Hierarchical Data Structures

Each person accumulates points, calculated daily for 14 days, according to how many people are below them

Tree::DAG_Node can help you to represent such structure quite easily. I've written an Introduction to Tree::DAG_Node where there is an example of such calculation made with the tree's methods.

To get you started, here is a sample hierarchy with its graphic representation and the score calculation. Swapping places and calculating optimum strategies is left as an exercise for you :).

#!/usr/bin/perl -w use strict; use Tree::DAG_Node; my $playerA = Tree::DAG_Node->new; $playerA->name('A'); $playerA->new_daughter->name('A1'); my $a2 = Tree::DAG_Node->new; $playerA->add_daughter($a2); $a2->name('A2'); $a2->new_daughter->name('A2a'); $a2->new_daughter->name('A2b'); $playerA->new_daughter->name('A3'); print map "$_\n", @{$playerA->draw_ascii_tree},"\n"; print $playerA->dump_names; my $playerB = Tree::DAG_Node->new; $playerB->name('B'); $playerB->new_daughter->name('B1'); my $b2 = Tree::DAG_Node->new; $playerB->add_daughter($b2); $b2->name('B2'); $b2->new_daughter->name('B2a'); $b2->new_daughter->name('B2b'); $b2->new_daughter->name('B2c'); my $b3 = Tree::DAG_Node->new; $playerB->add_daughter($b3); $b3->name('B3'); $b3->new_daughter->name('B3a'); my $b4 = Tree::DAG_Node->new; $b4->name('B4'); $b4->new_daughter->name('B4a'); $b4->new_daughter->name('B4b'); $b3->add_daughter($b4); $b3->new_daughter->name('B3b'); print map "$_\n", @{$playerB->draw_ascii_tree},"\n"; print $playerB->dump_names; $playerB->self_and_descendants, "\n"; print "A score: ", scalar $playerA->self_and_descendants, "\n"; print "B score: ", scalar $playerB->self_and_descendants, "\n"; print "B2 score: ", scalar $b2->self_and_descendants, "\n"; __END__ | <A> /--------+-------\ | | | <A1> <A2> <A3> /-----\ | | <A2a> <A2b> 'A' 'A1' 'A2' 'A2a' 'A2b' 'A3' | <B> /-----------+--------------------\ | | | <B1> <B2> <B3> /-----+-----\ /--------+--------\ | | | | | | <B2a> <B2b> <B2c> <B3a> <B4> <B3b> /-----\ | | <B4a> <B4b> 'B' 'B1' 'B2' 'B2a' 'B2b' 'B2c' 'B3' 'B3a' 'B4' 'B4a' 'B4b' 'B3b' A score: 6 B score: 12 B2 score: 4

I would recommend this module also because the documentation is exceptionally clear and informative. I've used Tree::DAG_Node in several projects.

 _  _ _  _  
(_|| | |(_|><

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://335744]
[Lady_Aleena]: Your Mother, how would that work? I'm having a hard time understanding the pod.
[Your Mother]: $dir->chidren, grep {} $file->slurp &c &c. :P
[Your Mother]: Stick with what you know. Or post a SOPW and if it's not complicated I guarantee someone will answer; me if it's straightforward and no one else gets it better first.
[1nickt]: ++YourMother (Grammar Vigilante of Bristol)
[Lady_Aleena]: Um, how does grep do multiline? print "$file:$_" if grep { /get_(array|hash| data)\(.+\)/ } $_; only returns the same 18 lines the command line grep does.
[Lady_Aleena]: my scratchpad has what I have so far.
[choroba]: what do you mean by multiline?
[Discipulus]: thanks 1nickt!

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2017-05-23 19:45 GMT
Find Nodes?
    Voting Booth?