Hi Marc, On Thu, 8 Sep 2011 08:58:09 -0700 Marc <sono...@fannullone.us> wrote:
> I'm trying to capitalize 3 and 4 letter words that contain only vowels > or consonants, but not both. The code I've come up with is not working > correctly. Given the string 'The Kcl Group', it should return 'The KCL > Group' but it is also capitalizing the word 'THE'. What am I doing wrong? > > Thanks, > Marc > > > use strict; > use warnings; > > my $string = 'The Kcl Group'; > > my @words = split(/ /, $string); > my @new_words; > foreach my $word (@words) { > if ((length $word >= 3 and length $word <= 4) and ($word !~ > m/[aeiouy]+/gi or $word !~ m/[bcdfghjklmnpqrstvwxz]+/gi)) { The problem here that /g confuses Perl and puts it in the \G anchor mode. Removing both /g fixes the problem. Some other notes: 1. You don't really need to say [...]+ inside the regex. [....] here would be enough. 2. You map opt to use perldoc -f map instead of foreach here. 3. Since the foreach aliases the variable you can modify the array inplace. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ UNIX Fortune Cookies - http://www.shlomifish.org/humour/fortunes/ Learn Perl from “Learning Perl in 24 Minutes Unleashed, in a Nutshell for Dummies.” — based on Shlomi Fish and f00li5h on #perl Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/