Sunday 1 March 2009

Counting totalwords,validwords, each word count in a file?

#!/usr/bin/perl

while (<>) {

foreach (split) {
$total++;
next if /\W/;
$valid++;
$count{$_}++;

}

}

print "total words= $total, validwords = $valid \n";
foreach $word (sort keys %count) {

print "$word was seen $count{$word} times.\n";

}

Note : We can pass any number of files to this program, as $lt;> - the diamond operator takes all the command line arguments (file names) specified.

No comments:

Post a Comment

Tweets by @sriramperumalla