CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [zapzap] Regex: Edit Log



Kenosis
User

Feb 3, 2014, 4:27 PM


Views: 3700
Re: [zapzap] Regex


Code
sub orderedII { 
join ' ', grep { my @x = sort my @y = /[aeiou]/gi; "@x" eq "@y" } split ' ', shift;
}


As your subroutine does, the above splits the sent string on whitespace, and the resulting list is sent to grep. Within the code block, a regex grabs all of the vowels in a word (if any) and initializes @y with them, and those are passed to sort and the sorted vowels are stored in @x. When an array is placed within double-quotes, it's interpolated and acts like a string with (usually) a space between elements. One array is sorted; the other is not and it's elements--the vowels--are in the order in which they appear in the word. If the two interpolated arrays are equal, the vowels in the word are in alphabetical order, and the word is passed through grep and joined with a space. The result of the join, if any, is returned.

I assumed that you wanted to let 'words' pass if they didn't contain any vowels, even though your spec says "...only those words that have vowels [aeiou] in alphabetical order." If vowels are a requirement, you can use the following at the end of grep's code block:

Code
/[aeiou]/i and "@x" eq "@y"



(This post was edited by Kenosis on Feb 3, 2014, 10:35 PM)


Edit Log:
Post edited by Kenosis (User) on Feb 3, 2014, 7:28 PM
Post edited by Kenosis (User) on Feb 3, 2014, 7:30 PM
Post edited by Kenosis (User) on Feb 3, 2014, 7:31 PM
Post edited by Kenosis (User) on Feb 3, 2014, 10:35 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives