On 3/24/06, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
>
> Dear list,
>
> I've got this code:
>
> # now grab sub_domains, if there are any
> my @sub_domains = keys( %{ $HKU->{$full_domain} } );
>
> # if there are sub-domains, we don't want the parent in the list
> if ($#sub_domains >= 0) {
>     pop( @ret_ary );
> }
>
> Where @ret_ary contains URL info gathered using functions from TieRegistry.
> The problem is that whether there is anything in @sub_domains or not, the
> pop() call gets executed.
>
> The question is, how can I determine if the keys() call returned
> anything--that is, if @sub_domains contains anything or if it's empty.

right now you're checking what the index of the last element of sub_domains is,
and that is ALWAYS going to be 0 or more, so that's broken.

A side-effect of arrays evaluating to the number of elts in them in
scalar context
is that you can test an array as a boolean condition and it will be
empty->false,
non-empty->true.  So good idioms include

    @ary and pop @ary;

    pop @ary if @ary;

    if(@ary){
                 pop @ary
    };

TMTOWTDI.

The cleanest patch, given the snippet you posted, is simply s/=>/>/
or maybe s/$#/@/. It looks like the author isn't quite sure what $# does,
since we have to presume that he knows the difference between > and =>.




--
David L Nicol
There's nothing to it but to do it

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to