Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: count words which contain all vowels in a file.

by Corion (Patriarch)
on Apr 01, 2010 at 07:13 UTC ( [id://832224]=note: print w/replies, xml ) Need Help??


in reply to count words which contain all vowels in a file.

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.

Replies are listed 'Best First'.
Re^2: count words which contain all vowels in a file.
by vennila (Novice) on Apr 01, 2010 at 07:48 UTC
    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>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found