homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

Counting words using getc

 4:49 am on Jan 24, 2009 (gmt 0)

As part of my assignment, I must count all the words in a text file but am struggling to do so. I managed to do get the file to count all lines, paragraphs, sentences and characters but not each word

Here is my code so far...

while ($line = <IN>) {
$l = $.; #lines
if ($line =~ m/^$/) {$p++}; #paragraph
if($line =~ /[?!.]/) {$s++} #sentences
$c+=length($line); #character

I tried to count the words using this code...

if($line =~ /\s/) {words++;}
...but this just counts the lines also. I tried other ways but have been unsucessful. Anyone can help?

EDIT: sorry thread should be not using getc as I can create it using getc but can't create lines and paragraph although yet to try fully

[edited by: phranque at 12:36 pm (utc) on Jan. 24, 2009]
[edit reason] disabled graphic smileys ;) [/edit]



 1:03 pm on Jan 24, 2009 (gmt 0)

welcome to WebmasterWorld [webmasterworld.com], steve802008!

site:webmasterworld.com perl count words - Google Search [google.com]

specifically from the suggested search, i would further suggest that you learn from what janharders wrote in the "Counting instances of word in text files. [webmasterworld.com]" thread and use that as the basis for your solution.
good basic perl building blocks in that post.

[spoiler alert: it probably needs a chomp]


 7:09 pm on Jan 26, 2009 (gmt 0)

Thanks for that. By using the google search (why didn't I do that before?) I was able to find a code from the stonehenge [stonehenge.com] website which went something like this...

while (<>) {
foreach $file (sort keys %count) {
print "$file has $count{$file} lines\n";

And I have been using the post from janhardees especially as my assignment is possibly the same as davo1977. I will post my code up here soon as I finish ammending the rest of it

[edited by: phranque at 12:46 pm (utc) on Jan. 27, 2009]
[edit reason] disabled graphic smileys ;) [/edit]


 12:51 pm on Jan 27, 2009 (gmt 0)

i thought you were counting words, not lines...


 5:17 pm on Jan 27, 2009 (gmt 0)

The article he linked to gets into that later. It starts with counting lines since that is the simplest thing to do and builds on the code from there. Its an example of the type of resourse I wanted to link steve to, written by Randall Schwartz.


 3:01 am on Feb 6, 2009 (gmt 0)

Bit of an old thread now but I just want to say thanks again for the help I received here. I did say I would post my full code so if anyone is interested heres my code...

open(IN, "<$file") or die "Can't open file \"$file\":$!";

###declare variables and read into file using a while loop###
my($read); #variable to read file
$para = 1; #increments paragraph everytime file is read

while ($read = <IN>) {

$line = $.; ###counts lines
if ($read =~ m/^$/) {$para++}; ###count paragraphs
if($read =~ /[?!.]/) {$sentence++} ###counts sentences
$chars+=length($read); ###counts each character

###create array and split words into array and count each word###
@words = split(/\W+/, $read);
$count{$ARGV} += @words;

print "Statistics for file \"$file\"\n";
print "Characters:\t $chars\n";
print "Words:\t\t $count{$read}\n";
print "Lines:\t\t $line\n";
print "Sentences:\t $sentence\n";
print "Paragraphs:\t $para\n";

And for the record, I was counting words and lines!

[edited by: phranque at 4:46 am (utc) on Feb. 6, 2009]
[edit reason] disabled graphic smileys ;) [/edit]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved