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

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.
  • Comment on Re: Html::Element, search for unnested elements. This works, but I don't like the test...
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Html::Element, search for unnested elements. This works, but I don't like the test...
by kwaping (Priest) on Jun 14, 2005 at 17:13 UTC
    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