CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Printing and Counting words

 



smsh
Novice

Mar 29, 2010, 11:21 AM

Post #1 of 16 (2002 views)
Printing and Counting words Can't Post

Hello guys,

I am new to this forum and I recently started to learn PERL. I've been reading some tutorials along with class 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.

This is my assignment for the week and I am just staring at the monitor clueless.

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

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

I appreciate the help guys.


7stud
Enthusiast

Mar 30, 2010, 1:41 AM

Post #2 of 16 (1993 views)
Re: [smsh] Printing and Counting words [In reply to] Can't Post

...then put your assignment aside and try something simpler. Declare an array with 3 words in it. Use a for loop to step through the elements of the array. If the word has an 'o' in it, print the word. When you post the code for that, we'll move on. When you post code, surround it with code tags so that it looks like this:


Code
use strict; 
use warnings;
use 5.010;

my $string = 'hello world';
print $string, "\n";

--output:--
hello world



(This post was edited by 7stud on Mar 30, 2010, 1:43 AM)


toolic
User

Mar 30, 2010, 9:35 AM

Post #3 of 16 (1985 views)
Re: [smsh] Printing and Counting words [In reply to] Can't Post

Many of the building blocks for your code can be found in the free online Perl documentation: http://perldoc.perl.org/perlintro.html.

Read through that quick intro, write some code, then post back here if you are still having troubles.


murugaperumal
Novice


Mar 30, 2010, 9:03 PM

Post #4 of 16 (1973 views)
Re: [smsh] Printing and Counting words [In reply to] Can't Post


Code
use strict; 
use warnings;

open (FH,"<file") or die "Can't Open $!";
my $count;
while(<FH>)
{
my @array=split;
foreach(@array)
{
if(/a|e|i|o|u/)
{
$count++;
}
}
}



smsh
Novice

Mar 30, 2010, 9:22 PM

Post #5 of 16 (1968 views)
Re: [murugaperumal] Printing and Counting words [In reply to] Can't Post

I did the 1st part actually but I am stuck with 2nd part.

I know that I need to use ^ and $ for the beginning and the end but I just don't know how to put it in the IF statement :/
Patience is a virtue.


MetalGear
Novice


Mar 30, 2010, 9:59 PM

Post #6 of 16 (1964 views)
Re: [smsh] Printing and Counting words [In reply to] Can't Post

http://www.sthomas.net/roberts-perl-tutorial.htm#77-BasicRegularExpressions

I m sure this will help...


Nila
User


Mar 30, 2010, 10:25 PM

Post #7 of 16 (1962 views)
Re: [smsh] Printing and Counting words [In reply to] Can't Post

Try the following code for your second part.


Code
use strict; 
use warnings;

open FH, "file.txt" or die "Can't Open the file : $!";
my $count;
while (<FH>)
{
my @array=split;
unless(/^\s*$/){
for (@array)
{
$count++ if(/^(\w{3})\w*\1$/);
}
}
}
print "No.Of.Words(Start and End with Same Three Characters)=$count\n";



(This post was edited by Nila on Mar 30, 2010, 10:25 PM)


smsh
Novice

Mar 30, 2010, 10:33 PM

Post #8 of 16 (1958 views)
Re: [Nila] Printing and Counting words [In reply to] Can't Post


In Reply To
Try the following code for your second part.


Code
use strict; 
use warnings;

open FH, "file.txt" or die "Can't Open the file : $!";
my $count;
while (<FH>)
{
my @array=split;
unless(/^\s*$/){
for (@array)
{
$count++ if(/^(\w{3})\w*\1$/);
}
}
}
print "No.Of.Words(Start and End with Same Three Characters)=$count\n";



It says "No.Of.Words(Start and End with Same Three Characters)=8"

8 means there are 8 out of them ?
Patience is a virtue.


Nila
User


Mar 30, 2010, 10:37 PM

Post #9 of 16 (1956 views)
Re: [smsh] Printing and Counting words [In reply to] Can't Post

Yeah,smsh you're correct.8 means 8 words out of them in the file start and end with same three characters.


smsh
Novice

Mar 30, 2010, 10:47 PM

Post #10 of 16 (1953 views)
Re: [Nila] Printing and Counting words [In reply to] Can't Post

How do I print the word tho? I need to do that too. I tried to add some $str but couldn't connect it. I know that I need to put it above the $count++ if blahblah... but couldn't :/
Patience is a virtue.


MetalGear
Novice


Mar 30, 2010, 11:01 PM

Post #11 of 16 (1951 views)
Re: [smsh] Printing and Counting words [In reply to] Can't Post


Code
 

$count++ if(/^(\w{3})\w*\1$/);

push @array1, $_;



after the loop, print the array1...


(This post was edited by anil mundewadi on Mar 30, 2010, 11:27 PM)


Nila
User


Mar 30, 2010, 11:23 PM

Post #12 of 16 (1947 views)
Re: [smsh] Printing and Counting words [In reply to] Can't Post

Try this,
Here no need of array as anil said.


Code
if(/^(\w{3})\w*\1$/) 
{
$count++;
print "Line no $. has $_\n" ;
}



(This post was edited by Nila on Mar 30, 2010, 11:23 PM)


MetalGear
Novice


Mar 30, 2010, 11:36 PM

Post #13 of 16 (1943 views)
Re: [Nila] Printing and Counting words [In reply to] Can't Post

LaughLaughLaughLaughLaughLaughLaugh


smsh
Novice

Mar 31, 2010, 9:30 AM

Post #14 of 16 (1932 views)
Re: [anil mundewadi] Printing and Counting words [In reply to] Can't Post

Nice this worked pretty well. Thank you so much.

I've been trying to do it with a backreference but I was so lost :/
Patience is a virtue.


murugaperumal
Novice


Apr 1, 2010, 2:22 AM

Post #15 of 16 (1917 views)
Re: [smsh] Printing and Counting words [In reply to] Can't Post


Code
  
use strict;
use warnings;

my $count;
while(<>)
{
if(/(a)+/ and /(e)+/ and /(i)/ and /(o)+/ and /(u)+/)
{
$count++;
}
}

print "The total count is:$count\n";



smsh
Novice

Apr 1, 2010, 8:13 AM

Post #16 of 16 (1910 views)
Re: [murugaperumal] Printing and Counting words [In reply to] Can't Post

Aight guys, I have another question for yall. Thank you so much for the first one.

Extra Credit: You are to write an interpreter in Perl for Rong’s Basic Basic (RBB) as explained in class. The BNF description for RBB and a test file called Fib.rbb are part of the RBB.zip file which is available in the Course Documents folder on blackboard. If you call your interpreter myIntp.pl, you would execute the program via perl myIntp.pl Fib.rbb

Following is called Fib.rbb and the code inside is;


Code
Print "A few Fibonacci numbers:" 
X = 1
Print X
Y = 1
Print Y
10 If Y >= 20
Goto 100
EndIf
Temp = X + Y
X = Y
Y = Temp
Print Y
Goto 10
100 End

Patience is a virtue.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives