Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

vowel_search

by chainsawed (Novice)
on Aug 17, 2001 at 00:59 UTC ( [id://105516]=CUFP: print w/replies, xml ) Need Help??

This program will search a file specified on the command line for any strings that contain all of "aeiou" in arbitrary places. It parsed a 12.3MB ascii text file matching 17000+ lines in 5 seconds.
#!/usr/bin/perl if (<>) { @dog = <>; foreach $cat (@dog) { if ($cat =~ /a/i && $cat =~ /e/i && $cat =~ /i/i && $cat =~ /o/i && $cat =~/u/i) { print "$cat"; } } }

Replies are listed 'Best First'.
Re: vowel_search
by CheeseLord (Deacon) on Aug 17, 2001 at 04:09 UTC

    Your code at present will disregard a matching string on the first line. In addition, you read in the entire file at once, sucking up a good deal of memory if the file is quite large. Here's another way of doing it:

    #!/usr/bin/perl -w use strict; # Or don't. See if I care. while (<>) { print if (/[Aa]/ && /[Ee]/ && /[Ii]/ && /[Oo]/ && /[Uu]/); }

    I only used the character classes there because (I hear) they're faster than /i matches. Could be wrong on that count, though.

    His Royal Cheeziness

Re: vowel_search
by $code or die (Deacon) on Aug 17, 2001 at 06:13 UTC
    Not sure I'd want to load 17000 lines into an array in one go. I'd rather do something like this:
    #!/usr/bin/perl while (<>) { if (/a/i && /e/i && /i/i && /o/i && /u/i) { print; } }
    update: Ahh, CheeseLord gave similar advice. Should have read all replies before posting :)

    Error: Keyboard not attached. Press F1 to continue.
      Thank you $code or die, Your optimization took my total parse time from 5.2 seconds with my code to 2.9 seconds. I bow to my perlmonk teachers.
Re: vowel_search
by runrig (Abbot) on Aug 17, 2001 at 04:08 UTC
    I wonder how this would benchmark since it doesn't rescan any part of the string:
    use strict; my $vowels="aeiou"; for my $str (qw(abcdefghijklmnopqrstuvwxyz missing_vowels)) { my $tmp_v = $vowels; LOOP: { if ($str =~ /\G[^$tmp_v]*([$tmp_v])/i) { $tmp_v =~ s/$1//i; print "Succeeded: $str\n" and last LOOP unless $tmp_v; redo LOOP; } print "Failed: $str\n" and last LOOP; } }
    Update:Well, its dog slow. If there's a ton of consonants at the beginning of the string, it might be worthwhile, but otherwise, forget it. Oh well, it was worth a try :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-29 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found