I am writing a program that takes a file, reads all the words as lower case, and then prints alphabetically the list of the all the words used, and the frequency of how often they appear in the program. i am using a simple file with just this:
You like this
you cant wait to do this
And I need the output to be:
"--- Word Count ---"
cant 1
do 1
like 1
this 2
to 1
you 2
Is there a simple function call that allows me to keep track of how often words are used? Below is my code. If someone can let me know if I'm on the right track, or if there is a simple tool I am unaware of, I'd really appreciate it. Thanks!
---------------
open(TT, $ARGV[0]) or die "Can't open $ARGV[0]. \n";
@lines = <TT>;
close(TT);
@ary = split / /, $lines[$i];
@ary = lc @ary;
@ary = sort @ary;
print "----\tWord \t Count ----\n";
print "$ary[0]\n";
for ($i =0; $i <=$#ary; $i++)
{
$tmp[$x] = lc ($ary[$i]);
if ($tmp[$x] == $tmp[$x-1])
{
$x++;
}
}
exit (0);
You like this
you cant wait to do this
And I need the output to be:
"--- Word Count ---"
cant 1
do 1
like 1
this 2
to 1
you 2
Is there a simple function call that allows me to keep track of how often words are used? Below is my code. If someone can let me know if I'm on the right track, or if there is a simple tool I am unaware of, I'd really appreciate it. Thanks!
---------------
open(TT, $ARGV[0]) or die "Can't open $ARGV[0]. \n";
@lines = <TT>;
close(TT);
@ary = split / /, $lines[$i];
@ary = lc @ary;
@ary = sort @ary;
print "----\tWord \t Count ----\n";
print "$ary[0]\n";
for ($i =0; $i <=$#ary; $i++)
{
$tmp[$x] = lc ($ary[$i]);
if ($tmp[$x] == $tmp[$x-1])
{
$x++;
}
}
exit (0);