输入一个目录名称,统计此目录下所有文件出现的单词及个数,并统计单词所属文件的个数:
#输入并获得目录
print "please input the directory: \n";
$dir=<STDIN>;
chomp($dir);
opendir(DIR,$dir)||die "cannot open the directory $dir";
chdir($dir)||die "cannot cd to $dir";
#读取目录下的文件名,并存入数组@file_array
$numf=0;
@file_array=();
foreach $name (sort readdir(DIR)){
$numf++;
push(@file_array,$name);
}
$old=shift(@file_array);
$old=shift(@file_array);
$numf=$numf-2;
#print @file_array;
#print $numf;
#读每一个文件,文件内容存入并取词存入@word[$j];
@os=();
@word=();
@wordsum=();
for($j=0;$j<$numf;$j++){
open(FILE,@file_array[$j])||die "cannot open the file: @file_array[$j] \n";
@file=<FILE>;
$fl=@file;
chomp(@file[0]);
$os[$j]=@file[0];
for($i=1;$i<$fl;$i++){
chomp(@file[$i]);
$os[$j]=$os[$j].@file[$i];
}
# print $os[$j];
print "\n after update: \n";
@temp=split(/\W+/,$os[$j]);
@wordsum=(@wordsum,@temp);
$nf=@temp;
@word[$j]=@temp[0];
for($k=1;$k<$nf;$k++){
@word[$j]=@word[$j]." ".@temp[$k];
}
# print @word[$j];
}
#print "\n the total words are: \n";
#print "@wordsum \n";
#利用HASH,先把TF设为0
foreach $w (@wordsum){
$words{$w}=0;
}
@difwords=keys(%words); #只取不相同的单词。
@difwords=sort @difwords;
#print %words;
#统计词的词数:
foreach $u (@wordsum){
$words{$u}++;
}
#print "\n the number of each word: \n";
| 论坛热门帖子: | [lch203] 写得蛮好的linux学习笔记(10-21) [黑马制造] 学习java的30个目标(10-19) [笑傲股林] 做测试半年了,有点迷茫,应该再学些什么提高自己的测试水平和测试能力呢?(10-19) [udp8589] 大家用google的来吱一声? 用百度的~~也来报道下?(10-18) [沂偌掳兆] 本人总结的一些认为C++比较经典的书籍,希望对大家有用(10-18) |
| TAG标签: | 统计 @file_array #print the @word @wordsum foreach |
注册
个人空间
