Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

count words which contain all vowels in a file.

by vennila (Novice)
on Apr 01, 2010 at 07:10 UTC ( [id://832222]=perlquestion: print w/replies, xml ) Need Help??

vennila has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, 1. Input: The file words.txt (it is a very very very long word file.) I need to Print and count all the words in the input file which contain all five vowels such as a, e, i, o, u in it(in any order). I need some help for this requirement. Thanks in advance....
  • Comment on count words which contain all vowels in a file.

Replies are listed 'Best First'.
Re: count words which contain all vowels in a file.
by Corion (Patriarch) on Apr 01, 2010 at 07:13 UTC

    I suggest you look in your course material and notes and get together with other students in your class.

    Other than this being homework, maybe you want to show what code you've already written and explain to us how it should work and where it fails to work for you.

      I have tried the following code. I need any other best way to do it.
      use strict; use warnings; open FH, "file.txt" or die "can't open file : $!"; my $count=0; while(<FH>) { my @array=split; for (@array) { if (/a/ && /e/ && /i/ && /o/ && /u/) # Checking th +e vowels by using if statement { # Printing and counting the words print "$_\n"; $count++; } } } print "No of words: $count";

        I don't think you really mean any other way . . .

        #!/usr/bin/env runhaskell module Main where import System (getArgs) import qualified Data.ByteString.Lazy.Char8 as L has_all_vowels :: L.ByteString -> Bool has_all_vowels word = map (\pred -> pred word) predicates == [True,True,True,True,True] where predicates = (map (\c -> L.elem c) "aeiou") words_with_all_vowels = filter has_all_vowels . L.lines main :: IO () main = do [infile] <- getArgs contents <- L.readFile infile mapM_ L.putStrLn $ words_with_all_vowels contents -- -- $ {time ~/vowels.hs /usr/share/dict/words} | wc -l -- ~/vowels.hs /usr/share/dict/words 0.80s user 0.04s system 99% cpu +0.852 total -- 5942 --

        Addendum: Mine doesn't handle AEIOU correctly, but then neither does the OP's.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

        I asked three things of you:

        1. code you've already written
        2. explain to us how it should work
        3. [explain] where it fails to work for you

        You have fulfilled step 1. Maybe now you feel ready to start with step 2?

        Your solution isn't bad. There are minor points of improvement (for instance, you dont't really need the variable @array), but why aren't you happy with your code?

        -- 
        Ronald Fischer <ynnor@mm.st>
Re: count words which contain all vowels in a file.
by Utilitarian (Vicar) on Apr 01, 2010 at 08:22 UTC
    Hi vennila, It's not that difficult a problem, break it into manageable sections
    • open the file words.txt
    • while the file is open examine each word
    • if a is present and e is present and i is present and o is present and u is present
    • print the current word, or push it onto an array you will return when you are finished
    Now each of those items is doable in a single command (or less), try and code it up and feel free to ask if you have a problem with your Perl code rather than your basic algorithm.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: count words which contain all vowels in a file.
by AnomalousMonk (Archbishop) on Apr 01, 2010 at 07:43 UTC

    For extra credit, print and count all words in the word list in which all the vowels appear (in any order) once and only once for each vowel.

      perl -nE"/(?=^[^a]*a[^a]*$)(?=^[^e]*e[^e]*$)(?=^[^i]*i[^i]*$)(?=^[^o]* +o[^o]*$)(?=^[^u]*u[^u]*$)/ and print" words.txt
Re: count words which contain all vowels in a file.
by BrowserUk (Patriarch) on Apr 02, 2010 at 00:29 UTC
    perl -nE"/(?=.*a)(?=.*e)(?=.*i)(?=.*o)(?=.*u)/ and print" words.txt |w +c -l
Re: count words which contain all vowels in a file.
by rovf (Priest) on Apr 01, 2010 at 07:23 UTC
    I need some help for this requirement
    Sure, just ask what kind of help!

    -- 
    Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-23 08:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found