Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > count lines,words,punctuations and characters in perl script

Reply
Thread Tools

count lines,words,punctuations and characters in perl script

 
 
angel
Guest
Posts: n/a
 
      11-11-2006
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!

 
Reply With Quote
 
 
 
 
angel
Guest
Posts: n/a
 
      11-11-2006
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 befrom 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:
heydiddlediddlethecatandthefiddlethecowjumpedovert hemoonthelittledoglaughedtoseesuchsportandthedishw anawaywiththespoon

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





http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> "angel" <(E-Mail Removed)> writes:
> > hello im a perl beginner.

>
> Hello. Welcome.
>
> > is there anybody who can help me by giving me
> > simple perl script?

>
> If you want to hire a programmer jobs.perl.org is down the hall. If
> you want help to write your own script, go read the posting
> guidelines, and then come back.
>
> > i need to know on how to count lines, words,
> > punctuations and characters of a pharagraph.

>
> This smells like a homework assignment. Do you really think your
> lecturer doesn't read this group?
>
> > pls i really need help
> > thanks a lot. i would really appreciate your help thanks!

>
> What work have you done thus far, and how has it failed to meet your
> needs?
>
> There was only one e. e. cummings -- you're not him. Use mixed case and
> try to spell. Writing (good) programs requires clarity and precision
> in any language. Practice being clear and precise in human languages,
> too.
>
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> Lawrence Statton - (E-Mail Removed) s/aba/c/g
> Computer software consists of only two components: ones and
> zeros, in roughly equal proportions. All that is required is to
> sort them into the correct order.


 
Reply With Quote
 
 
 
 
angel
Guest
Posts: n/a
 
      11-11-2006
thanks for the reply...im a pel beginner and i will be using perl when
i started making websites.

thanks a lot!

Michele Dondi wrote:
> On 11 Nov 2006 06:50:44 -0800, "angel" <(E-Mail Removed)> wrote:
>
> >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,


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-11-2006
angel <(E-Mail Removed)> wrote:

> 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 ]

--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
grocery_stocker
Guest
Posts: n/a
 
      11-11-2006

angel wrote:
> 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!


Two seconds of google yielded the following:

http://www.stonehenge.com/merlyn/UnixReview/col02.html

 
Reply With Quote
 
angel
Guest
Posts: n/a
 
      11-13-2006
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




grocery_stocker wrote:
> angel wrote:
> > 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!

>
> Two seconds of google yielded the following:
>
> http://www.stonehenge.com/merlyn/UnixReview/col02.html


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-13-2006
angel <(E-Mail Removed)> wrote:

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



You can replace that code with:

$_ = do { local $/; <READFILE> };


> $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?


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert HTML special characters to the real characters with a Java script Stefan Mueller HTML 3 07-23-2006 10:09 PM
Execute another perl script from my perl script Petterson Mikael Perl Misc 3 01-05-2005 01:31 PM
problem calling perl script from SOAP server perl script pj Perl Misc 3 04-09-2004 10:23 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM
How to make Perl Script "POST" call from another Perl Script??? Wet Basement Perl 1 07-15-2003 10:25 PM



Advertisments