Re: [問題] 大量字串資料個數出現次數統計
※ 引述《spider1216 (順著感覺走)》之銘言:
: 不好意思我是perl新手
: 我的問題是 我現在有404個單字資料庫
: 然後我要去比對一個文字檔案,其內容由上面資料庫中的單字組成
: 我想去統計我的文字檔案中 有哪些資料庫單字且出現幾次
: 請高手可以教我該怎麼做
==== FILE: dict ====
hello
you
how
do
are
==== FILE: sentence ====
How do you do ?
How are you ?
What is your name ?
==== FILE: count.pl ====
#!/usr/bin/perl
use strict;
use warnings;
my %dict = ();
my %unseen = ();
#### 讀入字典檔
open( DICT, "dict" );
while (<DICT>) {
chomp;
$dict{ lc($_) } = 0;
}
close(DICT);
#### 比對並計算出現次數
open( FH, "sentence" );
while (<FH>) {
chomp;
my @words = split( /[^a-zA-Z]+/, $_ );
foreach my $w (@words) {
if ( exists( $dict{ lc($w) } ) ) {
$dict{ lc($w) }++;
}
else {
$unseen{ lc($w) } = 0;
}
}
}
close(FH);
#### 輸出統計結果
print "$_ => $dict{$_}\n" foreach ( sort keys %dict );
print "UNSEEN: ", join( ", ", sort keys %unseen ), "\n";
# perl count.pl
are => 1
do => 2
hello => 0
how => 2
you => 2
UNSEEN: is, name, what, your
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.114.64.130
推
08/02 15:01, , 1F
08/02 15:01, 1F
推
10/01 10:16, , 2F
10/01 10:16, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):
Perl 近期熱門文章
PTT數位生活區 即時熱門文章