Monks, ok, I have this function that returns HTML::Element objects that satisfy a certain lookdown, and are not "self nested." See comments below for what I mean by self nested.

This seems to work, but I wanted the test for whether lookdown returned anything to be, is the @array an empty list. This doesn't work. But is $#array > 0 works. I thought the two were the same, but obviously not.

Basically, in the below code I want to have if ( @nested_same_kind_of_tag ) as a test instead of if ( $#nested_same_kind_of_tag ). But only the second one works. Am I missing something obvious?

Complete code:

# first arg is an HTML::Element, second arg is a tag to match with loo +kdown. # this should only return tags that don't have themselves nested somew +here inside. # ie, if checking <font color="red">blah <font color="red">red blah</f +ont> blah</font> # it should only return the inner font tag, not the outer. # it works, but I don't like the if test using $#. Is there a better +way? sub un_self_nested { my $element = shift or die "no element"; $element->isa('HTML::Element') or die "not an html element"; my %tag_match = @_ or die "no tag match"; my @unself_nested = $element->look_down( %tag_match, sub { my $no_mested_matches = 1; # initialize to no nested match +es, which is what we want. my $tag_matches = 0; my $tag = $_[0]; # current element my @nested_same_kind_of_tag = $tag->look_down( %tag_match +); my $nested_same_kind_of_tag = $#nested_same_kind_of_tag; if ( $nested_same_kind_of_tag ) { $no_mested_matches = 0; print "nested_same_kind_of_tag: " . $nested_same_kind_ +of_tag . "\n"; } return $no_mested_matches; # only match tags which don't c +ontain themselves as a nested match. } ); return @unself_nested; }

In reply to Html::Element, search for unnested elements. This works, but I don't like the test... by tphyahoo

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.