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]
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]
#!/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]
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]