Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by Roger (Parson)
on Feb 11, 2004 at 04:05 UTC ( [id://328140]=note: print w/replies, xml ) Need Help??


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

Good candidate for an OBFU. :-)

In the following example, $found is the number of unique vowels found in your string. Just test if it is equal to 5.
use strict; use warnings; my $word = "alighthousa"; my $found; if (($found = ( keys %{{map { $_ => 1 } ($word =~ m/([aeiou])/g) }} )) + == 5) { print "Found all 5 vowels\n"; } else { print "Found only $found vowels\n"; }

Ok, may be I should explain what is happenning here:

($word =~ m/([aeiou])/g) # returns a list of all vowels found in $word map { $_ => 1 } ($word =~ m/([aeiou])/g) # builds a list for initializing a hash { map { $_ => 1 } ($word =~ m/([aeiou])/g) } # a reference to an anonymous hash initialized with the list keys %{ { map { $_ => 1 } ($word =~ m/([aeiou])/g) } } # return keys of the dereferenced anonymous hash # the keys in a hash are unique, this effectively # eliminates duplicates, and gives a list of unique # vowels found in the string $found = ( keys ... ) # this assigns the number of unique vowels found into $found

Replies are listed 'Best First'.
Re^2: regex testing for ALL of the vowels in a scalar
by Anonymous Monk on Feb 11, 2004 at 05:05 UTC

    Very nice approach indeed. Just to touch up on including uppercase vowels:

    $_ = 'A Lighthouse'; if (5 == keys %{{map {lc $_ => undef} m/([aeiou])/ig}}) { print "Found at least one of each vowel.\n"; } else { print "Not all vowels were found.\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-24 03:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found