CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  其他开发语言 >  脚本语言(Perl ,Python)

TF,DF统计(perl),顶者有分

楼主ectt0923()2006-03-16 22:48:45 在 其他开发语言 / 脚本语言(Perl ,Python) 提问

小弟刚开始学PERL,用了一晚上实现以下功能:  
  输入一个目录名称,统计此目录下所有文件出现的单词及个数,并统计单词所属文件的个数:  
  #输入并获得目录  
  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";  
  print   %words;  
   
  ##new   thing.to   get   the   DF;  
  foreach   $f   (@difwords){  
  $df{$f}=0;  
  }  
  foreach   $p   (@difwords){  
  for($l=0;$l<$numf;$l++){  
  if(@word[$l]=~/\b$p\b/){  
  $df{$p}++;  
  }  
  }  
  }  
  print   "\n   the   df   is   :   \n";  
  print   %df;  
   
  #格式化输出:  
  format   TDF=  
  @<<<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<@<<<<<<<<  
  $names,$words{$names},$df{$names}  
  ============================================  
  .  
   
  open(TDF,">tdf.txt")||die   "canot   creat   tdf";  
  foreach   $names   (@difwords){  
  write   TDF;  
  }  
   
  想请教几个问题:  
  1.如何操纵多维数组,如a=((1,2,3),(2,3,4),(3,3,4)),如何表示a的第二个元素,即(2,3,4)  
  2,大家觉得perl的精华在什么地方,有人告诉我是正则表达式,HASH和引用,我却不认为引用有什么特别,比C中的引用功能强大吗?  
  3,欢迎高手简化以上程序.  
  问题点数:50、回复次数:3Top

1 楼xyzxyz1111(程序员的自我修养)回复于 2006-03-17 11:45:18 得分 45

用引用,把圆括号改成方括号  
  $a   =   [[1,2,3],   [2,3,4],   [3,3,4]];  
  @row2   =   @$a[1];  
  Top

2 楼michael_g_hu(一代风云)回复于 2006-03-17 13:54:31 得分 5

我顶就是了Top

3 楼ectt0923()回复于 2006-03-26 13:35:01 得分 0

没有人再提意见了吗?Top

相关问题

  • perl如何统计一个字符串的字符个数??
  • df
  • df
  • df.
  • 统计
  • 统计!!
  • 统计
  • 统计
  • 查询统计
  • 统计问题???

关键词

  • perl
  • 文件
  • numf
  • tdf
  • 引用
  • 存入
  • 目录
  • dir
  • die
  • array

得分解答快速导航

  • 帖主:ectt0923
  • xyzxyz1111
  • michael_g_hu

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo