这就是我所拥有的
my %count_words;
while (<DATA>){
my $line= $_;
chomp $line;
my @words = split (/ /, "$line");
foreach my $word(@words){
$count_words{"$word"}++;
}
}
foreach my $key (%count_words){
print "\"$key\" occurs \"$count_words{$key}\" times\n";
}
__DATA__
we name is something
this is what it does
we food food food food
这是我得到的输出
"it" occurs "1" times
"1" occurs "" times
"what" occurs "1" times
"1" occurs "" times
"name" occurs "1" times
"1" occurs "" times
"food" occurs "1" times
"1" occurs "" times
"does" occurs "1" times
"1" occurs "" times
"is" occurs "2" times
"2" occurs "" times
"we" occurs "2" times
"2" occurs "" times
"food" occurs "3" times
"3" occurs "" times
"something" occurs "1" times
"1" occurs "" times
"this" occurs "1" times
"1" occurs "" times
我的问题是为什么要创建这些附加键,它们本质上是先前创建的键-> 值对的值。
这是我所期望的
"it" occurs "1" times
"what" occurs "1" times
"name" occurs "1" times
"food" occurs "1" times
"does" occurs "1" times
"is" occurs "2" times
"we" occurs "2" times
"food" occurs "3" times
"something" occurs "1" times
"this" occurs "1" times
有人可以纠正我明显的错误吗?