Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Finding vowels

by bichonfrise74 (Vicar)
on Mar 20, 2009 at 01:25 UTC ( [id://751915]=note: print w/replies, xml ) Need Help??


in reply to Finding vowels

How about this?
#!/usr/bin/perl use strict; my @arrays = qw( chatterbox test abode); my @vowels = qw( a e i o u ); foreach my $i (@arrays) { my $count = 0; for (my $j = 0; $j <= (length $i) - 1 ; $j++) { my $letter = substr($i, $j, 1 ); $count++ if ( grep /\b$letter\b/, @vowels ); } print "$i has at least 3 or more vowels.\n" if ( $count >= 3 ); }

Replies are listed 'Best First'.
Re^2: Finding vowels
by linuxer (Curate) on Mar 20, 2009 at 01:46 UTC

    Hi, though I still prefer the tr/// solution, I think, a rewrite of your code could be helpful ;o)

    So, how about this (based upon your solution)?

    #! /usr/bin/perl use strict; use warnings; my @array = qw( chatterbox test abode ); my @vowels = qw( a e i o u ); # check each word in @array; so why using $i instead of $word? WORD: for my $word ( @array ) { my $count = 0; # split $word into characters CHARACTER: for my $character ( split //, $word ) { # I see no need for regex if checking each character against each +vowel # check if character is equal a vowel $count++ if grep { $_ eq $character } @vowels; # could be inserted to make it (maybe?; depends on the words which + are checked) faster #last CHARACTER if $count >= 3; } print "$word has at least 3 or more vowels.\n" if $count >= 3; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-28 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found