Perl 如何只计算只包含A-Z和A-Z的单词?

Perl 如何只计算只包含A-Z和A-Z的单词?,perl,Perl,好的,首先,这是我的代码: #!/usr/bin/perl use open qw(:utf8 :std); use utf8; print "Which file do you want to search?\n"; $file = <>; if ($file =~ /^\s*$/) { $file = "test.txt"; } open (FILE, $file) or die("Could not open file."); %hash; while

好的,首先,这是我的代码:

#!/usr/bin/perl

use open qw(:utf8 :std);

use utf8;

print "Which file do you want to search?\n";

$file = <>;

if ($file =~ /^\s*$/) {
    $file = "test.txt";
}

open (FILE, $file) or die("Could not open file.");

%hash;

while (<FILE>) {
    $hash{$_}++ for split /\W+/;
}

$count = 0;

for (sort {
        $hash{$b} <=> $hash{$a}
                  ||
           lc($a) cmp lc($b)
                  ||
              $a  cmp  $b
     } keys %hash )

{
    next unless /\w/;
    printf "%-20s %5d\n", $_, $hash{$_} if ($count <= 9);
    $count++;
}
如你所见,数字5被列出,这是不应该发生的

谢谢

++$hash{$_} for grep /^[a-zA-Z]+\z/, split /\W+/;
当然,你可能指的是只包含字母的单词

++$hash{$_} for grep /^\pL+\z/, split /\W+/;

非常感谢。是的,我的意思是只包含字母的单词:P在写了几个小时的代码后有点累了。我是perl新手,所以每一个小问题都需要阅读书籍和论坛—“为什么不
split/\Pl+/
?@Patrick J.S.,不是同等的。当OP代码没有时,会考虑<代码> ABC <代码> > ABC123DEF>代码>。
++$hash{$_} for grep /^\pL+\z/, split /\W+/;