I think what you are going to want to do is to make the $withclickiness variable no longer optional in descend_htmltree, since you are checking it in htmltree_to_html you really dont need to anyway as it will already be set or 0.

Also, I think you actually meant to be passing that along your recursive call to descend_htmltree anyway, otherwise it would only do it for the top node.

Then once this is all in place, add a third variable, which will be the node-id you are looking for. Each time you pass the $node_id to the next recursive call, it will get appended with the $node_counter, which is just the depth in the current child group. This should then produce the node_id you are looking for.

Here is some code, I could not test is as you supplied no test data to test against. But it should work, let me know if you have any problems with it.

sub descend_htmltree {   my $node = shift;   my $withclickiness = shift; my $node_id = shift; my $node_counter = 0;   foreach my $tmpnode (@{$node}) { $node_counter++     if(ref($tmpnode) eq 'HASH') {       my $nodeid = "${node_id}.$node_counter"; # Magic code to generat +e node's position in tree       $htmloutput .= "<div style='border: thin solid #bbbbbb' onDblCli +ck=\"alert('you clicked $nodeid')\">" if($withclickiness);       $htmloutput .= "<$tmpnode->{tag}";       foreach(keys %{$tmpnode}) {         $htmloutput .= " $_=\"$tmpnode->{$_}\"" if($_ ne 'tag' && $_ n +e 'content');       }       $htmloutput .= ">";       descend_htmltree($tmpnode->{content}, $withclickiness, $current_ +node_id);       $htmloutput .= "</$tmpnode->{tag}>";       $htmloutput .= "</div>" if($withclickiness);     } else {       $htmloutput .= "$tmpnode";     }   } }
One another note, I am not 100% sure your HTML, in particular the onClick part of the DIV tag will work properly. The browser may see the entire contents of the DIV (it and all its subtrees) as all part of the same HTML container.

-stvn

In reply to Re: tracking where I am in a tree structure by stvn
in thread tracking where I am in a tree structure by agaffney

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.