Hello, I am attempting to process data in a tree where, for example, "D" is the child of "C", "C" is the child of "B", and so on all the way back to "A".

Represented another way, the path to "D" is A/B/C/.

My goal is to create a subroutine that would return the path to whatever child I passed to the subroutine. So, once again, if I passed "D" to the subroutine, it would return "A/B/C/". I have approached this task by creating two hashes that share the same keys:

my %name = (1, "A", 2, "B", 3, "C", 4, "D"); my %parent = (1, 0, 2, 1, 3, 2, 4, 3);
I have also created a subroutine to return the value from the "name" hash for the parent of a child:
sub get_parent_value { my $parent_key = $parent{$key}; my $parent_value = $name{$parent_key}; return $parent_value; }
I am now uncertain how to create a loop that will keep passing the key of the child's parent to the subroutine until the parent of the parent is "0".

I would love to see any ideas you all might have on this.

Thanks for your time!


In reply to Processing Data in a Tree by reluctant_techie

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.