Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hello guys, I am new to this forum and I recently started to learn PERL. I've been reading some tutorials along with class that I am taking at school but ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    9

    Exclamation [Perl] Printing and counting vowels


    Hello guys,

    I am new to this forum and I recently started to learn PERL. I've been reading some tutorials along with class that I am taking at school but I am pretty lost about it. The slides from school doesn't make any sense so I started to search some decent forums to get some help with this program. I am just staring at the monitor clueless. I wanted to practice some stuff before my assignments were up so basically I am trying to stay ahead of the class but I didn't understand much. I found some practices but there is no answer sheet to it. I couldn't figure this one out.

    1. Input: The file words.txt (it is a very very very long word file.)

    Output: Prints and counts all the words in the input file which contain all five vowels a, e, i, o, u (in any order)

    2. Input: The file words.txt

    Output: Prints and counts all the words in the input file which begin and end with the same three letters.

    I appreciate the help guys.

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    So to be clear, this is just practice, not a homework assignment, right?

    For the first problem, it seems to me that you need to read in a word at a time, check if it contains the vowels, and print them. You can do this by simply using regular expressions if you really want:
    Code:
    if($str =~ /a/ && $str =~ /e/ && ...)
    For the second, you can do this by using backreferences, where you use data captured from the pattern earlier again. Do you know about regular expression capturing?

  3. #3
    Just Joined!
    Join Date
    Mar 2010
    Posts
    9
    Yes, it is only practice. I don't let people do my homework, otherwise what is the point of going to school.

    Like I said I've been into this about a week now basically I am a big noob of PERL.

    It will be word count like it says, and how many vowels in that word? That's what I understood.

    and also I figured out the if statement myself before you posted here but I just really don't know how to begin the program. I wish I knew C because it is close to C programming.

    Update: by the way, just realized that, if statement that you gave me won't work well tho, because u keep using " && ". So if there is e, a, i, o AND u in it, the statement will be true. Don't you think we need to use OR instead of AND? or nested if statement?

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Glad to hear that you do your own homework . You'd be surprised: we get a lot of people here who want us to do their homework for them.

    In your description for problem 1, you said that it has to contain all five vowels. Hence the '&&'.

    You might start the program this way:
    Code:
    #!/usr/bin/perl
    
    open(my $wordlist, "words.txt") or die "$0: Cannot open file \"words.txt\"";
    
    while(my $line = <$wordlist>)
    {
        # Now $line contains a word, so let's process it!
    }
    Do you understand how this works? We open the file and read it line-by-line.

    Note that I'm assuming here that words.txt contains one word per line. If it's more paragraphy, you'll need to split the words, which can be done with the "split" function.

  5. #5
    Just Joined!
    Join Date
    Mar 2010
    Posts
    9
    This is how I did mine actually. Figured it out after your post later on today lol. It works without any problems but if you could check it for me whatever is unnecessary I can change/remove it.

    Code:
    #!/usr/bin/perl -w
    use strict;
    
    # Opening the file containing file and putting it in the $file
    my $file = $ARGV[0];
    open (FILE, $file);
    
    # Variables
    my $str;
    my $counter = 0;
    
    # While loop until the file is done.
    while($str = <FILE>)
    {
               if ($str =~ /a/ && $str =~ /e/ && $str =~ /i/ && $str =~ /o/ && $str =~ /u/)     # Checking the vowels by using if statement
                  {
                    # Printing and counting the words
                    print "$str\n";
                    $counter++;
                  } else
                    {
                        # There is no vowels in the word, next word.
                         print 'There is no vowels.'
                    }
    }
    # Printing the counter
    print 'Total count is = ', "$counter\n";
    I couldn't figure out the #2 tho. I know that I only need to change the IF statement that I have and also since it is asking me to match THREE LETTERS, I know that I need to use " ^ " (for the beginning) and " $ " (for the end) but I just cannot connect them :/

  6. #6
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    It looks pretty good! I have two comments, however.

    First of all is the way you're opening your file. The usual way to open a file in Perl is:
    Code:
    open(FILE, $file) or die "$0: Could not open file $file\n"
    Note the "or die" part. If the file fails to open, you need to handle that error. The "\n" at the end of the error statement prevents Perl from printing a stack trace, which is generally unneeded except for the programmer.

    The second comment is just a personal style thing, but your final print statement would be more natural as:
    Code:
    print "Total count is = $counter\n";
    You can stick your variables right into the string, and it makes the string a lot easier to read.

    For the second one, you're right about using ^ and $. You'll also want to use those backreferences I mentioned. Here's an example of using backreferences:
    Code:
    tanya:perl alex$ cat backreferences 
    #!/usr/bin/perl -w
    
    use strict;
    
    print "foo matches\n" if "foo" =~ /(.{3})\1/;
    
    print "foofoo matches\n" if "foofoo" =~ /(.{3})\1/;
    
    print "foobarfoo matches\n" if "foobarfoo" =~ /(.{3})\1/;
    tanya:perl alex$ ./backreferences 
    foofoo matches
    By using \1 in the regex, I am able to refer to a group that I captured earlier in the regex.

    Does this make sense?

  7. #7
    Just Joined!
    Join Date
    Mar 2010
    Posts
    9
    I am lost actually lol but I think I got the part that you're checking the last and first 3 letters by using .[3] ?

    UPDATE: The program was working yesterday and now it is just repeating "There is no vowels" and Counter is 288. WTH!

    Update 2: Aha found it. Forgot to put " ; " (NVM wasn't it)

  8. #8
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    .{3} means 3 of anything. You could get the same result by using "...", but ".{3}" is a bit clearer.

    Do you know about regular expression capturing? \1, \2, etc. refer to captured groups.

  9. #9
    Just Joined!
    Join Date
    Mar 2010
    Posts
    9
    not at all. :/ didn't get to that yet I guess or missed it.

    btw, can u check what is wrong w/ my first program? it was working yesterday and right now it is not. it executes the if and gets the counter but displays else statement message. I am very confused and I couldn't find what it is wrong

  10. #10
    Just Joined!
    Join Date
    Mar 2010
    Posts
    9
    I figured out the 1st part. FIXED it actually.

    I am still stuck on the 2nd part tho. I just really don't know how to put it in the IF STATEMENT.

    Will something like this work;

    Code:
    my @array=split; 
        foreach(@array) 
        { 
            if(/a|e|i|o|u/) 
            { 
                $count++; 
            } 
        }

Page 1 of 2 1 2 LastLast

Posting Permissions

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