SitePoint Sponsor |
|
User Tag List
Results 1 to 2 of 2
Thread: perl program problem
-
Jul 31, 2001, 20:43 #1
- Join Date
- Jul 2001
- Location
- Asian region
- Posts
- 4
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
perl program problem
I have a problem
with my perl program - it is not able to display the output of vowel count and word repetition count from a text file.
The whole Perl program is supposed to read a paragraph of text and calculate followings based on the text:
1. number of empty spaces.
2. number of words in the text.
3. number of vowels in the text (a,e,i,o,u)
4. number of words with repetition in the text
Example output:-
No. of empty spaces : XX
No. of words : XX
No. of vowels -
a: : XX
e : XX
I : XX
o : XX
u : XX
Words with repetition-
Word 1 : XX
Word 2 : XX
..... ....
..... ....
Here below is the program so far:
#####################
print ("Enter file to be analyzed : ");
$database = <STDIN>;
chop $database;
open(MYFILE,$database);
#--------------------------calculate empty spaces
while ($line = <MYFILE>) {
$line =~ s/[^ ]//g;
@blank = split(//, $line);
$array = @blank;
$arraysize = $arraysize + $array;
#--------------------------calculate no. of words
@words = split(//,$line);
$words = @words;
$wordcount = $wordcount + $words;
#-------------------------calculate no. of vowels
$line =~ s/[^aeiou]//g;
@vowels = split(//, $line);
foreach $vowel (@vowels)
{
$vowelcount{$vowel} += 1;
}
#---------------------calculate repetitive words
@words = split(//,$line);
$m = 0;
foreach $words(@words)
{
if ($m ne 0)
{
$n = $m;
while ($n > 0)
{
if ($words[$m] eq $words[$n-1]) {
$wordcount{$words} +=1;
}
%repwords = ("$words",$wordcount{$words});
$n--;
}
}
$m++;
}
}
##----------------------------------output format
$~ = "OUTPUTFORMAT";
write;
format OUTPUTFORMAT =
================================================
Number of empty spaces : @<<<<<
$arraysize
Number of words : @<<<<<
$wordcount
Number of vowels found in text file :
a: @<<<<< e: @<<<<<
$vowelcount{"a"}, $vowelcount{"e"}
i: @<<<<< o: @<<<<<
$vowelcount{"i"}, $vowelcount{"o"}
u: @<<<<<
$vowelcount{"u"}
Number of words with repetition :
foreach $key (keys %repwords) {
print "$key : $repwords{$key}\n"; }
===============================================
.
#################################
I use ActivePerl to compile/interpret and execute the codes, but it produces errors - wrong number count and no number shown on screen for vowel count and for words with repetition.
Could you please help debug the errors to achieve the correct output and advise me (explanation) on my errors? Thank you very much.
-
Jul 31, 2001, 22:00 #2
- Join Date
- Apr 2001
- Location
- BC, Canada
- Posts
- 630
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
wrong forum man..there is one specifily for perl
Bookmarks