Perl统计英文单词(没有考虑缩写和连写词)

这篇博客介绍了如何使用Perl进行英文单词统计。通过正则表达式匹配单词并利用哈希表存储,展示了简单的Perl代码实现,并给出了运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

英文中单词之间直接有空格,而且Perl利用正则直接能匹配上,同时perl考虑可以用hash表存储,而C++ 可以考虑用STL map存取。随便在intermediate perl教程上copy了一段文字,下面是perl 的简单实现方法:


my @data = <DATA>;
my $data = join('', @data);
my %words;
while ($data =~ m#\b(\w+)\b#smg)
{
if (not exists $words{$1})
{
$words{$1} = 1;
}
else
{
$words{$1}++;
}
}


for my $key ( sort keys %words)
{
print "$key => $words{$key}\n";
}


__DATA__
Each new release of the debugger works slightly differently than previous releases, so
our screen might not look exactly like what we show here. Also, if we get stuck at any
time, we can type h for help, or look at perldebug.
The debugger shows each line of code before it executes it. That means that, at this
point, we’re about to invoke the autovivification, and we’ve got our keys established.
The s command single-steps the program, while the x command dumps a list of values
in a nice format. We can see that $source, $destination, and $bytes are correct, and
now it’s time to update the data:


运行结果:

Also => 1
Each => 1
That => 1
The => 2
We => 1
a => 2
about => 1
and => 3
any => 1
are => 1
at => 3
autovivification => 1
before => 1
bytes => 1
can => 2
code => 1
command => 2
correct => 1
data => 1
debugger => 2
destination => 1
differently => 1
dumps => 1
each => 1
established => 1
exactly => 1
executes => 1
for => 1
format => 1
get => 1
got => 1
h => 1
help => 1
here => 1
if => 1
in => 1
invoke => 1
it => 3
keys => 1
like => 1
line => 1
list => 1
look => 2
means => 1
might => 1
new => 1
nice => 1
not => 1
now => 1
of => 3
or => 1
our => 2
perldebug => 1
point => 1
previous => 1
program => 1
re => 1
release => 1
releases => 1
s => 2
screen => 1
see => 1
show => 1
shows => 1
single => 1
slightly => 1
so => 1
source => 1
steps => 1
stuck => 1
than => 1
that => 2
the => 5
this => 1
time => 2
to => 2
type => 1
update => 1
values => 1
ve => 1
we => 5
what => 1
while => 1
works => 1
x => 1




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值