I agree with geek491, using regular expressions would simply complicate matters. This is one of those problems that you could solve with a masterful single regular expression, but you'd be wasting your time to bother writing it, and wasting computer resources to run it. Instead, working off of geek491's code, you can solve with the following code:
-
#!/usr/bin/perl
-
-
my $inFile = $ARGV[0] or die "no file specified";
-
-
open(IN, $inFile) or die "open $inFile: $!";
-
-
my $wordCount = 0;
-
my $count = 0;
-
-
while (my $line = <IN>) {
-
while ($line =~ m{([a-z]+)}ig) {
-
my $word = $1;
-
$wordCount++;
-
if ($word =~ m{[^aeiou]{4}}i) {
-
print "$word\n";
-
$count++;
-
}
-
}
-
}
-
-
print "$count of $wordCount have 4 consonants\n";
-
-
close(IN) or die "close $inFile: $!";
-
-
1;
-
-
__END__
-
You'll notice two important changes to the code that I provided. First off, using split to determine the words in a line does not work completely because it will include punctuation characters. Secondly, I've included the code to test for 4 consonants. As there is not a character class in perl for this, I simply used "not a vowel" to test for this. I've also included a little counter to verify that this code actually ran just in case your text doesn't include any such words.
Finally, you'll have to decide for yourself if "y" is a vowel. You can include the word "demonstration" if your text doesn't have any long words and you want to verify that this script works.