On Sat, 08 Mar 2003 17:31:08 -0800, R. Joseph Newton <[EMAIL PROTECTED]> wrote:

"Scott R. Godin" wrote:

> #!/usr/bin/perl -w
>
> use strict;
>
> my @array = ('', 0, "", 1);
>
> my $hasTruth;
> foreach (@array) {
>   if ($_ ){
>     $hasTruth = "yezzindeedydo!";
>     last;
>   }
> }
> if ($hasTruth) {print "$hasTruth has truth\n";}
> else {print "Does not have truth";}
>
>
> Joseph

#!/usr/bin/perl
use warnings;
use strict;

my @array = ('', 0, "", 1);

foreach (@array) {
print $_ ? "$_ has truth\n" : "$_ has no truth\n;
}

The problem is that you kept the bsth water and through out the baby. The original poster was interested only in empty lists. The print statements at the end of my code were only debug code to indicate the value assigned to the variable $hasTruth, which was the payload.


The design, which may have seemed heavy to you, served a purpose by providing shortcut evaluation on failure--finding a true element in the list. Since you can never make a shortcut evaluation in affirming a categorical [this list has no valid elements, this list is all valid elements] the appropriate efficiency is to shortcut out on negation.

Joseph



This question got me wondering if last would work in grep, as in


# assume same preliminaries and array as above...

if (grep { $_ && last } @array) {
 # ....
}

It does not.

This does, but it is not as efficient as the loop with last. If the array is always small or empty much more often than not, then it wouldn't matter.

if (grep { $_ } @array) {
 # ....
}

The idea of last for grep has apparently been suggested for Perl 6

http://nntp.x.perl.org/group/perl.perl6.announce/266

Does anyone know the status of this or, better yet, where to find out? I haven't been following Perl 6 development.

Katy





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to