Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: regex testing for ALL of the vowels in a scalar by davido
in thread regex testing for ALL of the vowels in a scalar by D'Oh!!

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found