2014-10-03 35 views
0

因此,我有一個簡單的腳本來從命令行讀取文本文件,並且我想要計算「the」的數量,但我已經得到奇怪的數字。計數發出給我所有不同答案的單詞「the」

while(<>){ 
    $wordcount= split(/\bthe\b/, $_);} 
    print "\"the\" occurs $wordcount times in $ARGV"; 

因此,使用,我得到的10個事件,但如果我用/ \的意見書\ B/I我得到12/\的意見書\ B /給我6,我相信。我的測試txt中有11次出現。我只是一個白癡? $ wordcount應該從1還是0開始?用這種方法分裂也是不好的做法嗎?該代碼適用於實際計數的單詞,但不是在計算確切的字符串時。新的perl所以任何和所有的虐待感激。謝謝

編輯:我也知道它沒有添加,但現在我得到$ wordcount被視爲更像一個數組,所以它爲以前的迭代工作,雖然它肯定是糟糕的形式。

+0

您正在覆蓋每行的'$ wordcount'。所以你只打印最後一行的出現次數。如果你想要總數,你應該使用'+ ='而不是'='。 – Barmar 2014-10-03 02:55:51

+0

他們都是錯的。兩者都是因爲你沒有添加(就像Barmar說的那樣),並且因爲'split'沒有任何方法來計算匹配模式的事物的數量(它通常會但不總是太高)。 – hobbs 2014-10-03 03:07:12

回答

2

split根據提供的正則表達式將字符串拆分爲一個列表。您的計數來自您已將split置於標量環境中的事實。從perldoc -f split

split Splits the string EXPR into a list of strings and returns the 
     list in list context, or the size of the list in scalar context. 

鑑於字符串「敏捷的棕色狐狸跳過懶狗」我期望你$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是整個字符串(全局),不區分大小寫。

+0

不知道我可以使用()作爲一個數組。感謝您提供豐富的答案 – 2014-10-03 03:26:18

+0

@D_C請參閱['perlsecret'](http://search.cpan.org/dist/perlsecret/lib/perlsecret.pod#Goatse)以獲取更多信息。 – chrsblck 2014-10-03 05:48:09

+0

@chrsblck gotse讓我大笑,但另外解釋了所有的原因和我的過程。謝謝! – 2014-10-03 07:17:34

1

使用/ i標誌,'The'將被包含,但不是沒有它。

\ B是 -word邊界,所以只能找東西像「穿衣」,並「的」。

是的,以這種方式使用分割是不好的做法。正確,如果你只是想一個數,這樣做:

$wordcount =() = split ...; 

分標量方面做一些事情,似乎是一個不錯的主意最初,但似乎並沒有那麼好了,所以避免它。上面的咒語在列表上下文中調用它,但將找到的元素的數量分配給$ wordcount。

分裂the產生的元素不是你想要的;你想要找到the的次數。所以,做(可能與/ IG,而不是隻/ G):

$wordcount =() = /\bthe\b/g; 

請注意,你可能想+ =,不等於,一共拿到了所有行。

0

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}; 

上面的代碼獲得輸入VAI命令提示,然後搜索詞到給定的文本文件「sample.txt的」,然後顯示它多少次出現在文本文件中的輸出(樣本.txt)

注意:用戶輸入必須是「區分大小寫」。從用戶

INTPUT

Enter your string = Ajith 

輸出

$VAR1 = 'Result: 4'; 
0
print "Enter the string: "; 
chomp($string = <>); 
die "Error opening file" unless(open(fil,"filename.txt")); 
my @file = <fil>; 
my @mt; 
foreach (@file){ 
@s = map split,$_; 
push(@mt,@s); 
} 
$s = grep {m/$string/gi} @mt; 
print "Total no., of $string is:: $s\n"; 

在此給您所期望的輸出。

相關問題