Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2003
    Location
    san jose, CA
    Posts
    68

    Unanswered: usage of regular expresson?

    Dear forums members:

    I am doing a exercise which requires me to find out all the words that contains five vowels(a, e, I, o, u) from a text file.

    I wrote a the following code to test it.
    While(<>)
    {
    print if /aeiou/
    }

    but the result didn’t show any words that contains five vowels. I am wondering what’s wrong the my regular expression. Please give me some suggestions. Thanks!
    sjgrad03
    12-02-03

  2. #2
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $word = "receive";
    $_ = $word;
    print "$word\n" if (tr/aeiouAEIOU//d == 4);
    Help?

  3. #3
    Join Date
    Mar 2004
    Posts
    114
    Or simply (for the regex)

    Code:
    m/[aeiou]/i
    The [] seems : one of the letter inside
    The /i seems : non case sensitive

  4. #4
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    Code:
    #!/usr/bin/perl
    
    # Derived from
    # aeiou <- a test "word" so you can run the program on itself 
    
    use strict;
    use warnings;
    
    if (@ARGV == 1) {                                # We have what should be the file of words
      open (INPUT,"<$ARGV[0]") or die "$!\n";
      $/=undef;                                      # I like to slurp so
      foreach (split(/\s+/,<INPUT>)) {               # Process each of the "words" --- you said you'd be given a text file so I'll assume (my bad) that's the case!
        print "m: $_\n" if (m/^[^aeiou]*([aeiou][^aeiou]*){5}$/i); # Exactly FIVE vowels you say?
        print "tr:$_\n" if (tr/aeiouAEIOU/aeiouAEIOU/ == 5); # the faster way
         };
      close (INPUT);                                 # We're done close up
       }
    else {
      print "Invoke as\n$0 <filename>\n";
       };

  5. #5
    Join Date
    Aug 2003
    Location
    san jose, CA
    Posts
    68

    Thanks for the help

    Hello Senaz:

    Thanks for your reply! The requirements is to catch all the words that contains all five vowels (e.g. Vaxeiou).

    I will try the code see if it works.
    sjgrad03
    12-03-04


    Quote Originally Posted by senza_nome
    Code:
    #!/usr/bin/perl
    
    # Derived from
    # aeiou <- a test "word" so you can run the program on itself 
    
    use strict;
    use warnings;
    
    if (@ARGV == 1) {                                # We have what should be the file of words
      open (INPUT,"<$ARGV[0]") or die "$!\n";
      $/=undef;                                      # I like to slurp so
      foreach (split(/\s+/,<INPUT>)) {               # Process each of the "words" --- you said you'd be given a text file so I'll assume (my bad) that's the case!
        print "m: $_\n" if (m/^[^aeiou]*([aeiou][^aeiou]*){5}$/i); # Exactly FIVE vowels you say?
        print "tr:$_\n" if (tr/aeiouAEIOU/aeiouAEIOU/ == 5); # the faster way
         };
      close (INPUT);                                 # We're done close up
       }
    else {
      print "Invoke as\n$0 <filename>\n";
       };

  6. #6
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    Let me see if I have this correct? The word must "use" each of the vowels (a,e,i,o,u)? And something that merely has five "a"s (Oooooh) is NOT what you want?

    In that case, my mistake. The code above finds "words' that have exactly five vowels. It doesn't discriminate between them. An "A" is as good as an "E"!

    Code:
    #!/usr/bin/perl
    
    # Derived from
    # aeiou <- a test "word" so you can run the program on itself
    
    use strict;
    use warnings;
    
    if (@ARGV == 1) {                                # We have what should be the file of words
      open (INPUT,"<$ARGV[0]") or die "$!\n";
      $/=undef;                                      # I like to slurp so
      foreach (split(/\s+/,<INPUT>)) {               # Process each of the "words" --- you said you'd be given a text file so I'll assume (my bad) that's the case!
        print "tr:$_\n" if (tr/aA/aA/ & tr/eE/eE/ & tr/iI/iI/ & tr/oO/oO/ & tr/uU/uU/) ; # each of the vowels (a,e,i,o,u) must appear one or more times
         };
      close (INPUT);                                 # We're done -  close up
       }
    else {
      print "Invoke as\n$0 <filename>\n";
       };
    Last edited by senza_nome; 12-03-04 at 14:07.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •