Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: regex testing for ALL of the vowels in a scalar

by davido (Cardinal)
on Feb 11, 2004 at 05:08 UTC ( [id://328150]=note: print w/replies, xml ) Need Help??


in reply to regex testing for ALL of the vowels in a scalar

Here's my take on it. Someone's going to do a pure regex solution... but not me. Still, this seems like a pretty simple to follow solution, so it might be useful to you.

use strict; use warnings; my $string = "Understand, this string really contains all of the vowel +s."; my $count; $string =~ /$_/i and $count++ for qw/ a e i o u y /; print "They're all there!\n" if $count == 6;

The meat and potatos of this solution is in the $string =~ /$_/i and... line. Start with the "for qw/ a e i o u y /" part. ...That creates a little loop that distributes each vowel, one by one, to $_. Next, the string is pattern matched with the contents of $_ (each vowel, one by one). The logical short circuit operator ("and") ensures that $count is only incremented when a match occurs. So each time through the loop you're testing one vowel to see if it's found in the string, and incrementing $count if the vowel exists. Then you loop back, and check for the existance of the next vowel in the list.

The hardest part is the logical short circuit "and". Just think of it like this... for 'and' to be true, both the left hand side and the right hand side must be true. If the left hand side isn't true (ie, if there's no match), there's no point in even evaluating the right hand side, since the 'and' operator already knows that it's going to fail (return false). That means that $count++ only gets evaluated if the match succeeds, and thus, $count only gets incremented if the match is successful.

Hope this helps...


Dave

Replies are listed 'Best First'.
Re:x2 regex testing for ALL of the vowels in a scalar
by grinder (Bishop) on Feb 11, 2004 at 08:55 UTC

    Indeed, this is the approach I had in mind. My idea was to look for the least frequently occurring vowel first, up to the most frequently occurring vowel last. That way candidates could be excluded as quickly as possible. The following one-liner, when run on /usr/share/dict/words

    perl -nle '++$f{lc $_} for split //; END{ print "$_\t$f{$_}" for sort keys %f}'

    ... allows me to determine that the order is u o a i e. My test, relying on the benefit of lazy evaluation, would be:

    # assuming target word is in $_ my $all_there = /u/ and /o/ and /a/ and /i/ and /e/;

    Disclaimer about case sensitivity: you might want to $_ = lc $_ beforehand, or add the i modifier to each RE.

    I find this code is very elegant: clear and straightforward, and above all, the person coming along behind you will understand immediately what is going in without requiring major regexp-fu.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://328150]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-26 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found