> -----Original Message-----
> From: Chris Charley [mailto:[EMAIL PROTECTED] 
> Sent: Friday, February 04, 2005 10:21 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'.
> 
> Chris 
> 

Thanks for the prompt reply, Chris

My mind has the same problem with grep that it does with map.  I don't
know why.  Maybe it's just doing too much at once for me to wrap my
brain around.  What exactly does that grep statement do?  I've read the
perldoc on it, and I still can't grasp it.  Let me try restating what it
does:  grep will iterate through each item in the list, returning a new
list composed of items that the block (or expression) returns true.  Is
that right?  So:
  
  my @new_list = grep {1} @orig_list;

Would set @new_list = @orig_list, and:
  
  my @new_list = grep {0} @orig_list;

Would set @new_list = empty.  Right?  And, in the block (or expression),
$_ will interpolate to each item in the @orig_list.  Have I got it
right?  Also, used in a scalar context, it's gonna give me the number of
returned items, right?  So in your example above, it will break when
there are 2 false values in the original hash?

Nope.  I tried it and it still works with more than one false value.

Can you help me understand what that grep is doing?

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