I assume there is a reason for the numbers? Otherwise you could just store names as keys and values, i.e. you would need only one hash
my %parent = ('A',0,'B','A','C',B','D','C';
That way keys %parent would give you a list of all names (as substitute for the names hash). If you don't need that, the hash entry ('A',0) could be dropped and not exists $parent($x) would be true if $x has no parent.

But if you need the numbers, there is still one thing I would change: get_parent_value should return the number of the parent and not the name (as long as it is fed the number of the child and not the name). It is not good design to continually switch between different representations of the same thing. You're sure to mix them up eventually and get a number when you expected a name or vice versa in your code

By the way, your sub is missing a first line my ($key)=@_;


In reply to Re: Processing Data in a Tree by jethro
in thread 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.