count lines,words,punctuations and characters in perl script

A

angel

hello im a perl beginner. is there anybody who can help me by giving me
simple perl script? i need to know on how to count lines, words,
punctuations and characters of a pharagraph. pls i really need help
thanks a lot. i would really appreciate your help thanks!
 
A

angel

hello thanks for the reply...where can i find that posting guidelines?

i just need someone to help me with my script. im not really a
programmer. perl is just one part of my course and its really my first
time to join a group i just find it very interesting and educational.

i already done half of my script but its the counting of the
lines,words etc that i dont have any idea.

here's my script:

#!c:\perl\bin\perl.exe -w

open(READFILE, "<StringText.txt"> || die "couldn't open file$!";

while (<READFILE>)
{
print "$_";
}
close(READFILE);
print"\n\nOutput:\n";

open(READFILE, "<StringText.txt"> || die "couldn't open file:$!";
while(<READFILE>)
{
while($_=~s/(\w+)(.*)/$2/)
{
$word= $2;
$wordHash{$word}++;
}
while(($word, $count) = each(%wordHash))
{
$wordArray[$i] = "$word\t$count";
$i++;
chomp($count);
print("words: $count\n");

close(READFILE);

print"\n\nString:\n";

open(READFILE, "<StringText.txt"> || die "couldn't open file:$!";

while(<READFILE>)
{
$_=~s/\s+//gi;
$_=~s/\,//gi;
$_=~s/\.//gi;
$_=~tr/A_Z/a-z/;
print"$_";
}
close(READFILE);

the output should be:(from READFILE)

Hey, diddle, diddle,
The cat and the fiddle,
The cow jumped over the moon.
The little dog laughed
To see such sport,
And the dish ran away with the spoon.

Output:
lines: 6
words:30
characters: 154

String:
heydiddlediddlethecatandthefiddlethecowjumpedoverthemoonthelittledoglaughedtoseesuchsportandthedishwanawaywiththespoon

i was able to do the first and the last (string) but its the counting
that im having difficulty.
 
A

angel

thanks for the reply...im a pel beginner and i will be using perl when
i started making websites.

thanks a lot!

Michele said:
hello im a perl beginner. is there anybody who can help me by giving me
simple perl script? i need to know on how to count lines, words,

Yes, and chances are that some will do.
punctuations and characters of a pharagraph. pls i really need help
thanks a lot. i would really appreciate your help thanks!

However... are you a perl beginner or do you need to use perl for some
reason for once only? If the latter, than specify why: if it's a good
reason then it may be easier for some to pretend this is a help desk.
But generally we're not, and in the former case we prefer to ask you
to show us what you've tried thus far and what you're having
difficulties with.

General purpose pointers:

perldoc -q 'by paragraph'
perldoc -f split
perldoc -f m
perldoc -f length


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
T

Tad McClellan

angel said:
where can i find that posting guidelines?


They are posted to this newsgroup twice each week. Or see:

http://www.augustmail.com/~tadmc/clpmisc.shtml

here's my script:

#!c:\perl\bin\perl.exe -w

open(READFILE, "<StringText.txt"> || die "couldn't open file$!";
^
^

That is not a Perl program.

Why aren't you asking about the syntax error that that lines generates?

Is this your real code?

print "$_";

perldoc -q vars

What’s wrong with always quoting "$vars"?

while($_=~s/(\w+)(.*)/$2/)


A loop that must iterate zero or one times is the same as an "if",
so then, it should be written as an "if".

If you expect it to iterate more than one time, then you need
a "g" modifier on your s/// operator.

while(($word, $count) = each(%wordHash))
{
$wordArray[$i] = "$word\t$count";
$i++;


If you use the push() function instead of indexing yourself, then you
won't need to maintain $i.

push @wordArray, "$word\t$count";



[ snip TOFU ]
 
A

angel

Hey, didle,didle,
The cat cat and the fiddle,
The cow jumped over the moon.
The little dog laughed
To see such sport,
And the dish ran away with the spoon.

Output:
lines: 6
words: 30
characters: 154

i did some program but the output is different.

i tried this:

open(READFILE, "Stringtext.txt") || die "couldn't open files:$!";

@_ = <READFILE>;
$_ = join("",@_);
$l_count = tr/\n//;
$w_count = tr/A-Za-z//;
$c_count = tr///cd;
chomp($l_count);
print("lines:" .$l_count,"\n");
chomp($w_count);
print("words:" .$w_count,"\n");
chomp($l_count);
print("characters:" .$c_count,"\n");

close(READFILE);

the output that i got when i run that program is:

Output
lines: 7 #should be 6
words:116 # should be 30
characters:152 # should be 154

i dont know how it turned up that way...i need help..thanks
 
T

Tad McClellan

angel said:
@_ = <READFILE>;
$_ = join("",@_);


You can replace that code with:

$w_count = tr/A-Za-z//;


tr/// works on _characters_, not on "words".

You need a different operator.

chomp($w_count);


Why do you expect a newline at the end of $w_count's value?

If you don't expect a newline, then why are you trying to remove one?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top