> -----Original Message-----
> From: Chris Charley [mailto:[EMAIL PROTECTED] 
> Sent: Friday, February 04, 2005 10:45 AM
> To: beginners@perl.org
> Subject: Re: Variable-sized hash of booleans
> 
> 
> > You can use grep.
> >
> > my %hash = (ONE => 1, TWO => 0, THREE => 1);
> >
> > if (grep {! $hash{$_}} keys %hash) {
> > print "false\n";
> > }
> > else {
> > print "true\n";
> > }
> >
> > Prints 'false'.
> 
> Guess it would be helpful to explain how grep works here. 
> From the perlfunc 
> man page:
> Evaluates the BLOCK or EXPR for each element of LIST (locally 
> setting $_ to 
> each element) and returns the list value consisting of those 
> elements for 
> which the expression evaluated to true. In scalar context, 
> returns the 
> number of times the expression was true.
> 
> In the code above, ! $hash{$_} evals to true when $hash{TWO} 
> is evaluated, 
> so grep, being in scalar context, would return 1, the number 
> of elements in 
> %hash which evaluated to 'true '.If all values were 'true', 
> (1's), then grep 
> would return 0 since !$hash{$_} would be false for every 
> element in that 
> case.
> 
> Chris
> 

Oh!

I got my true/false 's backwards cause of the '!' in there.  Thanks for
the explanation Chris.

--Errin

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to