in reply to regex testing for ALL of the vowels in a scalar
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 |