perl中统计单词出现频率的一段代码,大家帮我看看我那里错了?
来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/07/30 20:55:38
perl中统计单词出现频率的一段代码,大家帮我看看我那里错了?
/usr/bin/perl
open(MY,"1.txt");while(){
while(/fred/){
$words {$1}++;
} }
foreach $_ (keys %words){
print "$_$words{$1}\n;
"}
我的txt文本是
fred
barney
fred
dino
wilma
fred
/usr/bin/perl
open(MY,"1.txt");while(){
while(/fred/){
$words {$1}++;
} }
foreach $_ (keys %words){
print "$_$words{$1}\n;
"}
我的txt文本是
fred
barney
fred
dino
wilma
fred
![perl中统计单词出现频率的一段代码,大家帮我看看我那里错了?](/uploads/image/z/448144-16-4.jpg?t=perl%E4%B8%AD%E7%BB%9F%E8%AE%A1%E5%8D%95%E8%AF%8D%E5%87%BA%E7%8E%B0%E9%A2%91%E7%8E%87%E7%9A%84%E4%B8%80%E6%AE%B5%E4%BB%A3%E7%A0%81%2C%E5%A4%A7%E5%AE%B6%E5%B8%AE%E6%88%91%E7%9C%8B%E7%9C%8B%E6%88%91%E9%82%A3%E9%87%8C%E9%94%99%E4%BA%86%3F)
#!/usr/bin/perl
use strict;
use warnings;
my %words =();
open(MY,"1.txt")
or die "File problem:$!";
while (){
$words {$1}++ if /(\w+)/ ;
}
foreach (keys %words) {
print $_,"=> ",$words{$_},"\n";
}
再问: 能解释下这个代码吗? $words {$1}++ if /(\w+)/ ;
再答: ## $words {$1}++ if /(\w+)/ ; -- 如果一行 (在 $_ 里) 匹配到了一个单词,if ($_=~ m/(\w+)/ ) {...} 匹配到的单词在 $1 里 $words{$1} 就加 1 (起始数为0)
再问: 那您能帮我看下我哪里错了嘛,呵呵 ,我感觉我的也挺对的啊?谢谢您了
再答: --最大的错误: use strict; use warnings;
use strict;
use warnings;
my %words =();
open(MY,"1.txt")
or die "File problem:$!";
while (){
$words {$1}++ if /(\w+)/ ;
}
foreach (keys %words) {
print $_,"=> ",$words{$_},"\n";
}
再问: 能解释下这个代码吗? $words {$1}++ if /(\w+)/ ;
再答: ## $words {$1}++ if /(\w+)/ ; -- 如果一行 (在 $_ 里) 匹配到了一个单词,if ($_=~ m/(\w+)/ ) {...} 匹配到的单词在 $1 里 $words{$1} 就加 1 (起始数为0)
再问: 那您能帮我看下我哪里错了嘛,呵呵 ,我感觉我的也挺对的啊?谢谢您了
再答: --最大的错误: use strict; use warnings;