tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Html::Element, search for unnested elements. This works, but I don't like the test...
by davidrw (Prior) on Jun 14, 2005 at 16:51 UTC | |
by kwaping (Priest) on Jun 14, 2005 at 17:13 UTC |