Welcome to WebmasterWorld Guest from 54.163.25.166

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Counting words using getc

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

New User

5+ Year Member

joined:Jan 15, 2009
posts: 6
votes: 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)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


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)

New User

5+ Year Member

joined:Jan 15, 2009
posts:6
votes: 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...

#!/usr/bin/perl
while (<>) {
$count{$ARGV}++;
}
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)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


i thought you were counting words, not lines...
5:17 pm on Jan 27, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 20, 2008
posts:92
votes: 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)

New User

5+ Year Member

joined:Jan 15, 2009
posts:6
votes: 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;
}

close(IN);
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]