Welcome to WebmasterWorld Guest from 54.163.35.238

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Counting words using getc

     

steve802008

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

5+ Year Member



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]

phranque

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

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



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]

steve802008

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

5+ Year Member



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]

phranque

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

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



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

krugs

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

5+ Year Member



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.

steve802008

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

5+ Year Member



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]

 

Featured Threads

Hot Threads This Week

Hot Threads This Month