Perl 计算单词“quot”的出现次数;";给我所有不同的答案

Perl 计算单词“quot”的出现次数;";给我所有不同的答案,perl,Perl,因此,我有一个简单的脚本,可以从命令行读取文本文件,我想计算“the”的数量,但我得到了一些奇怪的数字 while(<>){ $wordcount= split(/\bthe\b/, $_);} print "\"the\" occurs $wordcount times in $ARGV"; while(){ $wordcount=split(/\b/,$\u);} 打印“\”该\”在$ARGV中出现$wordcount次; 所以使用它我得到10次,但是

因此,我有一个简单的脚本,可以从命令行读取文本文件,我想计算“the”的数量,但我得到了一些奇怪的数字

    while(<>){
    $wordcount= split(/\bthe\b/, $_);}
    print "\"the\" occurs $wordcount times in $ARGV";
while(){
$wordcount=split(/\b/,$\u);}
打印“\”该\”在$ARGV中出现$wordcount次;
所以使用它我得到10次,但是如果我使用/\b\b/I我得到12次/\b我相信他给了我6分。在我的测试文本中出现了11次。我只是个白痴吗?$wordcount应该从1或0开始吗?这样使用拆分也是一种不好的做法吗?该代码在实际计算单词时效果良好,但在计算精确字符串时效果不佳。perl是新手,因此任何滥用都值得赞赏。谢谢


编辑:我也知道它没有添加,但现在我知道$wordcount更像是一个数组,所以它在上一次迭代中起作用,尽管它的形式肯定很糟糕。

如果使用/I标志,则会包括“the”,但不能没有它

\B是一个非单词边界,因此只能找到像“Cloth”这样的词,而不能找到“the”

是的,这样使用拆分是不好的做法。正确地说,如果您只想计数,请执行以下操作:

$wordcount = () = split ...;
标量上下文中的拆分最初似乎是一个好主意,但现在似乎不再那么好了,所以要避免它。上面的咒语在列表上下文中调用它,但将找到的元素数指定给$wordcount

但是在
上拆分所产生的元素不是您想要的;您需要计算找到
的次数。也可以这样做(可能使用/ig而不仅仅是/g):


请注意,您可能希望+=,而不是=,以获得所有行的总数。

使用/i标志时,将包括“the”,但不包括它

\B是一个非单词边界,因此只能找到像“Cloth”这样的词,而不能找到“the”

是的,这样使用拆分是不好的做法。正确地说,如果您只想计数,请执行以下操作:

$wordcount = () = split ...;
标量上下文中的拆分最初似乎是一个好主意,但现在似乎不再那么好了,所以要避免它。上面的咒语在列表上下文中调用它,但将找到的元素数指定给$wordcount

但是在
上拆分所产生的元素不是您想要的;您需要计算找到
的次数。也可以这样做(可能使用/ig而不仅仅是/g):


请注意,您可能希望+=,而不是=,以获得所有行的总数。

split
根据提供的正则表达式将字符串拆分为一个列表。您的计数来自您在标量上下文中放置了
split
。发件人:

给定字符串“敏捷的棕色狐狸跳过懒狗”,我希望您的
$wordcount
为2,这是正确的

The quick brown fox jumps over the lazy dog
^^^============================^^^=========  -> two fields
然而,如果你有“一只鸟和一只敏捷的棕色狐狸跳过了一只懒狗”,你会得到3,这是不正确的

A bird and the quick brown fox jumps over the lazy dog
===========^^^============================^^^========= -> three fields
首先,您绝对需要
\b
,因为它与单词边界匹配
\B
匹配非单词边界的内容,因此您可以匹配包含“the”而不是“the”的任何单词

其次,您只需要计算出现的次数—您可以通过计算整个字符串的匹配项来完成

$wordcount = () = $string =~ /\bthe\b/gi

$wordcount
成为标量上下文中的列表,
()
是一个您不需要匹配项的列表,因此无法实际捕获
$string
是要匹配的字符串。您正在匹配单词边界处的“the”,并且
gi
是整个字符串(全局),不区分大小写。

split
根据提供的正则表达式将字符串拆分为一个列表。您的计数来自您在标量上下文中放置了
split
。发件人:

给定字符串“敏捷的棕色狐狸跳过懒狗”,我希望您的
$wordcount
为2,这是正确的

The quick brown fox jumps over the lazy dog
^^^============================^^^=========  -> two fields
然而,如果你有“一只鸟和一只敏捷的棕色狐狸跳过了一只懒狗”,你会得到3,这是不正确的

A bird and the quick brown fox jumps over the lazy dog
===========^^^============================^^^========= -> three fields
首先,您绝对需要
\b
,因为它与单词边界匹配
\B
匹配非单词边界的内容,因此您可以匹配包含“the”而不是“the”的任何单词

其次,您只需要计算出现的次数—您可以通过计算整个字符串的匹配项来完成

$wordcount = () = $string =~ /\bthe\b/gi

$wordcount
成为标量上下文中的列表,
()
是一个您不需要匹配项的列表,因此无法实际捕获
$string
是要匹配的字符串。您正在匹配单词边界处的“the”,并且
gi
是整个字符串(全局),不区分大小写。

在列表上下文中使用正则表达式来提取匹配的计数:

my $wordcount = 0;

while (<>) {
    $wordcount += () = /\bthe\b/g;
}

print qq{"the" occurs $wordcount times in $ARGV\n};
my$wordcount=0;
而(){
$wordcount+=()=/\b\b/g;
}
打印qq{“the”在$ARGV中出现$wordcount次\n};

参考:

在列表上下文中使用正则表达式来提取匹配的计数:

my $wordcount = 0;

while (<>) {
    $wordcount += () = /\bthe\b/g;
}

print qq{"the" occurs $wordcount times in $ARGV\n};
my$wordcount=0;
而(){
$wordcount+=()=/\b\b/g;
}
打印qq{“the”在$ARGV中出现$wordcount次\n};

参考资料:sample.txt

Ajith
kumar
Ajith
my name is Ajith and Ajith
lastname is kumar

代码

use Data::Dumper;

print "Enter your string = ";
my $input = <>; ## User input
chomp $input; ## The chomp() function will remove (usually) any newline character from the end of a string

my %count;
open FILE, "<sample.txt" or die $!; ## To read the data from a file
my @data = <FILE>;

for my $d (@data) {
    my @array = split ('\s', $d); ##To split the more than one word in a line
    for my $a (@array) {
        $count{$a}++;     ## Counter
    }
}

print Dumper "Result: " . $count{$input};
输出

$VAR1 = 'Result: 4';

sample.txt

Ajith
kumar
Ajith
my name is Ajith and Ajith
lastname is kumar

代码

use Data::Dumper;

print "Enter your string = ";
my $input = <>; ## User input
chomp $input; ## The chomp() function will remove (usually) any newline character from the end of a string

my %count;
open FILE, "<sample.txt" or die $!; ## To read the data from a file
my @data = <FILE>;

for my $d (@data) {
    my @array = split ('\s', $d); ##To split the more than one word in a line
    for my $a (@array) {
        $count{$a}++;     ## Counter
    }
}

print Dumper "Result: " . $count{$input};
输出

$VAR1 = 'Result: 4';
print“输入字符串:”;
chomp($string=);
模具“打开文件时出错”,除非(打开(fil,“filename.txt”);
my@file=;
我的@mt;
foreach(@file){
@s=地图分割,$\ux;
推送(@mt,@s);
}
$s=grep{m/$string/gi}@mt;
打印“$string的总数为::$s\n”;
在这里,给出您期望的输出。

print“输入字符串:”;
chomp($string=);
模具“打开文件时出错”,除非(打开(fil,“filename.txt”);
my@file=;
我的@mt;
foreach(@file){
@s=地图分割,$\ux;
推送(@mt,@s);
}
$s=grep{m/$string/gi}@mt;
打印“$string的总数为::$s\n”;

在这种情况下,给出您期望的输出。

您正在覆盖eac上的
$wordcount