Perl:计算单词

Perl: Counting words

我上周才开始学习 Perl。

我必须在这个目录中有两个文件。但稍后我可能想在同一目录中添加 100 多个 txt 文件。这段编码的目的是统计每个词出现的频率。

File1:
e
f
g 
h 
a 

File2:
a 
b 
c 
d 
e 

为了方便计数,我需要将所有的文本文件读入同一个数组中。

这是我的脚本。

my $dir = 'C:\Perl_Example\Data\New';
my %count;
my $line;

foreach my $fp (glob("$dir/*.txt")) 
{
    open my $fh, "<", $fp or die "can't read open '$fp': $OS_ERROR";
    #open file to read which is each file in dir
        
    while (my $line = <$fh>) #line by line to array
    {
        chomp $line;
        
        foreach my $str ($line =~ /\w+/g) 
        {
            $count{$str}++;
            printf "%-31s %s\n", $str, $count{$str};
        }
    }
}

我预计结果会像

f                               1
g                               1
h                               1
a                               2
b                               1
c                               1
d                               1
e                               2

却变成了这样

e                               1
f                               1
g                               1
h                               1
a                               1
a                               2
b                               1
c                               1
d                               1
e                               2

请指教

在处理完所有文件之前不要打印任何内容。

foreach my $fp (glob("$dir/*.txt")) 
{
    open my $fh, "<", $fp or die "can't read open '$fp': $OS_ERROR";
    #open file to read which is each file in dir
        
    while (my $line = <$fh>) #line by line to array
        {
        chomp $line;
        
        
        foreach my $str ($line =~ /\w+/g) 
            {
                $count{$str}++;
            }
            
        }
}

# We have now processed all of the files and have all of
# our data in %count.

for (keys %count) {
    printf "%-31s %s\n", $_, $count{$_};
}