I'd have a look at HTML::Element's dump method, as it already prints those addresses as you propose at the end of each tag. Try $tree->dump to see what I mean

Or you can roll your own like this:

#!/pro/bin/perl use strict; use warnings; use HTML::TreeBuilder; my $tree = HTML::TreeBuilder->new; my @l; $tree->parse_content (<DATA>); foreach my $e ($tree->look_down (_tag => qr{.})) { push @l, -1; splice @l, 1 + $e->depth; $l[-1]++; $e->attr ("addr", join ".", @l); } print $tree->as_HTML (undef, " ", {}); __END__ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <html> <head> <title>Testing SNMP addr</title> <meta name="Content-type" content="text/html;charset=utf-8"> <meta name="Generator" content="elvis 2.2"> <meta name="Author" content="H.Merijn Brand"> </head> <body> <h1>Hello world</h1> <p>Some content</p> </body> </html>

That would give me:

<html addr=0> <head addr="0.0"> <title addr="0.0.0">Testing SNMP addr</title> <meta addr="0.0.1" content="text/html;charset=utf-8" name="Content +-type"> <meta addr="0.0.2" content="elvis 2.2" name="Generator"> <meta addr="0.0.3" content="H.Merijn Brand" name="Author"> </head> <body addr="0.1"> <h1 addr="0.1.0">Hello world</h1> <p addr="0.1.1">Some content</p> </body> </html>

Edit: removed conditionals inside for loop


Enjoy, Have FUN! H.Merijn

In reply to Re: Walk through HTML::Treebuilder by Tux
in thread Walk through HTML::Treebuilder by perlmonkdr

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.