All: In this node, Isanchez asked for some code that would allow entering an arbitrary start point and return a string of decendents. While there were many fine answers, they were "tree" based and not flat. I came up with following monstrosity:
package Isanchez; use strict; use Carp; our $VERSION = '0.01'; sub new { my ($class, $file) = @_; my $self = bless {}, $class; $self->_build($file); return $self; } sub _build { my ($self, $file) = @_; croak "Unable to retrieve data" unless open(INPUT,$file); while (<INPUT>) { my @field = split; unless (@field == 3 && $field[0] =~ /^\d+$/ && $field[0] =~ /^ +\d+/) { carp "Unable to parse\n$_"; next; } $self->{$field[2]}{decendent} = $field[0]; push @{$self->{decendents}[$field[1]]}, $field[2]; } } sub get_decendents { my ($self, $origin) = @_; croak "You must enter an origin in the tree" unless $origin; croak "$origin can not be found" unless exists $self->{$origin}; my $index = $self->{$origin}{decendent}; my @list = @{$self->{decendents}[$index]} if exists $self->{decend +ents}[$index]; return $origin unless @list; my @decendents = ($origin); while (@list) { push @decendents, @list; my @temp; for my $decendent (@list) { my $index = $self->{$decendent}{decendent}; push @temp, @{$self->{decendents}[$index]} if exists $self +->{decendents}[$index]; } @list = @temp; } return join ', ' , @decendents; } #### Example Usage: #### #!/usr/bin/perl -w use strict; use Isanchez; my $family_tree = Isanchez->new('file.dat'); print $family_tree->get_decendents('dog'), "\n"; # Where file.dat looks like __END__ 1 0 domestic_animal 2 1 dog 3 2 terrier 4 2 collie 5 3 fox_terrier
While this works as requested, the get_decendents method looks very amaturish to me. Before I go on to create the other methods I dreamed up:
  • get_acendants
  • get_decendants_tree # Same data in tree format
  • get_acendants_tree # Same data in tree format

    I was hoping one of you fine monks could show me a more elgeant/efficient/perlish solution.

    Cheers - L~R


    In reply to Code Review (recursive data structure) by Limbic~Region

    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.