Script that lists all words used in one or more files and displays their count (pattern /\W+/ to split the lines of the input file into words can b used)..
It should display list in format word:count...gets Filename as an cmd line argument!
eg: $perl test doc (where doc is d file we are going to use)
is:200
and:50
why i should reinvent the wheel ?!
45 sub wc
46 {
47 my ($fname)=@_;
48 my $lines=0;
49 my $words=0;
50 my $chars=0;
51
52 open(FILE, $fname) || die "Couldn\'t open file $fname";
53 foreach my $line (<FILE>) {
54
55 $chars+=length($line);
56 $line=~/\n$/ and ++$lines;
57
58
59 $line=~s/^[ \t]*//g;
60 $line=~s/[ \t\r\n]*$//g;
61 my @w=split(/[ \t]+/, $line);
62 $words+=@w;
63 }
64
65 close(FILE);
66
67 return ($lines, $words, $chars);
68 }
From here: Download code - LiteratePrograms
with explanation: Word count (Perl - LiteratePrograms)
Homework?
99.99% homework
I have edited and gave you reason in private message!