diotalevi, Thanks - let me work through your points:
  • Returning string instead of @descendants is un-perlish
         Agreed. Because the desired output was a string I encorporated that into the module. It would have been better i guess to show the usage as print (join ',' $family_tree->get_decendents('dog')) , "\n"; and just returned @descendants
  • Your solution was nice
         But it didn't meet Isanchez's requirements about being a single line of output starting at an arbitrary point.
  • Infinite loop
         yes, if a node has a pointer to a child that is actually a parent - it will cause an infinite loop. That was one of the things I was working on when I decided it was just "yucky" code.
  • equivalent of symbolic references
         I don't understand this statement
  • Better variable names
         Could not agree more
  • My use of key assignment
         I disagree with your synopsis of what I did (but could very will just be misunderstanding you). I did not assign the id to the decendents, it is the array index where the decendents can be found. As far as the possibility that I may step on myself if I get a title that is 'decendents' (I think that is what you meant) - I could move it to a private base key.
  • hash "index" versus array for memory purposes
         I agree
  • I will have to fix get_decendents myself
         This is the piece of code I was hoping to get help on :-)

    How does this look like for an update?

    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 (my $line = <INPUT>) { chomp $line; my($id, $parent_id, $title) = split /\s+/ , $line, 3; unless ($title && $id =~ /^\d+$/ && $parent_id =~ /^\d+/ && $p +arent_id < $id) { carp "Unable to parse\n$line"; next; } $self->{$title}{decendent} = $id; push @{$self->{decendents}{$parent_id}}, $title; } } 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) { my $decendent = shift @list; push @decendents, $decendent; my $index = $self->{$decendent}{decendent}; push @list, @{$self->{decendents}{$index}} if exists $self->{d +ecendents}{$index}; } return @decendents; }

    Cheers - L~R


    In reply to Re: Re: Code Review (recursive data structure) by Limbic~Region
    in thread 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.