Thanks ... i figured the logic out the first time round except that I could
not see how the for is called before the regexp is done.  But I see it now ...

i was going to suggest a solution using map actually like
        print "yipe\n" if map {/dd_\w+/} keys %hash;

not sure if this would be better ... try running some benchmark on it ...

On Fri, Aug 24, 2001 at 12:13:34PM -0400, Bob Showalter shaped the electrons to read:
> > -----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]
> 

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

Reply via email to