tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:

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; }

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
    if ( @arr ) should work, but it might be clearer to say if ( scalar @arr ) instead. Also remember that $#arr gives the last index in the array, which is 0 if it is a one-element array. So really if( $#arr ) is equivalent to if ( scalar(@arr) > 1 ) Update: if ( scalar(@arr) != 1) lesson is still stick w/scalar @arr EndUpdate which is not what you want in this case.
      Not exactly. If the array @arr is undefined, $#arr == -1, which actually equates to true (being a real, non-zero value). The real equivalent is if ($#arr >= 0).

      Generally, if I want to check for the existance of an array containing values, I check for the existance of the first member: if ($arr[0]). However, even that isn't a perfect test (see examples below).
      #!/usr/bin/perl use strict; use warnings; my @asdf = (); print "hi\n" if ($#asdf); print "howdy\n" if (scalar @asdf); print "hola\n" if ($asdf[0]); print "---\n"; @asdf = ('a'); print "hi\n" if ($#asdf); print "howdy\n" if (scalar @asdf); print "hola\n" if ($asdf[0]); print "---\n"; @asdf = ('','a'); print "hi\n" if ($#asdf); print "howdy\n" if (scalar @asdf); print "hola\n" if ($asdf[0]); __END__ Output: hi --- howdy hola --- hi howdy