我在 Perl 中的问题是:Perl 脚本将读取文本(没有标点符号或数字)并打印出排序的单词列表以及每个单词在文本中出现的次数。我的脚本是:
#!/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 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
但我认为 SORTING 部分输出是错误的。单词出现部分输出正确。请帮忙。提前致谢