> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Friday, August 24, 2001 11:54 AM
> To: Bob Showalter
> Cc: '[EMAIL PROTECTED]'
> Subject: Re: test presence of hash key with regexp?
> 
> 
> Hmm i dont get how 
>     /add_\w+/ and print("Yippie!\n"), last for keys %hash;
> works ... the regexp will be checking $_ right?

   /add_\w+/ and print("Yippie!\n"), last for keys %hash;
                                          ^^^^^^^^^^^^^^^
                                          This iterates over
                                          the keys, setting
                                          $_ to each key

   /add_\w+/ and print("Yippie!\n"), last for keys %hash;
   ^^^^^^^^^
   This checks $_ and
   evaluates to true
   (scalar context)
   if a match found

   /add_\w+/ and print("Yippie!\n"), last for keys %hash;
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
             If a match is found, this part
             is evaluated. First the print,
             then the last. last just stops
             the for loop. Note use of "and"
             to achieve lower precedence than
             comma operator.

Really, grep() is probably the more straightforward way
to write this, and is what should probably be used:

   print "Yippee!\n" if grep(/add_\w+/, keys %hash);

But grep() builds a list of all matches (unless the Perl
optimizer can optimize this away), while the other solution
doesn't.

This might be more efficient still, since the list of keys 
wouldn't need to be created:

   /add_\w+/ and print("Yippie!\n"), last while ($_, undef) = each %hash;


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

Reply via email to