My question in Perl is:
Perl Script which will read the text (no punctuation or numbers) and print out a sorted list of words and the number of
times each occurs in the text.
My script is:
#!/usr/bin/perl
@names = qw(My name is Ashok Rao and I am the son of Shreesha Rao and Shailaja Rao);
join(',',@names);
my %count;
foreach (@names)
{
if (exists $count{$_})
{
$count{$_}++;
}
else
{
$count{$_} = 1;
}
}
my @order = sort(@names);
print "The words after sorting are:\n";
print "@order\n";
print "The number of times each word occurring in the text is: \n";
foreach (keys %count)
{
print "$_ \t = $count{$_}\n";
}
The Output is:
The words after sorting are:
Ashok I My Rao Rao Rao Shailaja Shreesha am and and is name of son the
The number of times each word occurring in the text is:
the = 1
son = 1
of = 1
name = 1
Ashok = 1
Shailaja = 1
is = 1
Rao = 3
am = 1
My = 1
I = 1
and = 2
Shreesha = 1
But I think the SORTING part output is wrong. Word occurrence part output is correct. Please help.
Thanks in advance
perl makes a distinction between lower and upper case.
You can use the
ucandlcfunctions to convert a string to upper or lower case.