Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

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


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

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";

Replies are listed 'Best First'.
Re^3: count words which contain all vowels in a file.
by Fletch (Bishop) on Apr 01, 2010 at 14:34 UTC

    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.

Re^3: count words which contain all vowels in a file.
by Corion (Patriarch) on Apr 01, 2010 at 08:25 UTC

    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?

Re^3: count words which contain all vowels in a file.
by rovf (Priest) on Apr 01, 2010 at 13:22 UTC
    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://832231]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found