perl文本处理总结[2]

来源:互联网 发布:惠普1415网络链接 编辑:程序博客网 时间:2024/05/18 01:22

7. 文件成行读入
   把一个文件读入一个字符串
方法:
 my $contents;
{
  local $/;
  $contents = <IN>;
}

my $contents = do {local $/; <IN>}
例子: 文件中以空行为语块分割的标记,把语块内部顺序保留,而语块间逆序输出。
#!
open IN,"input.txt";
open OUT,">output.txt";
my $contents;
{
  local $/;
  $contents = <IN>;
}
my @all = split("/n/n",$contents);#不同文件格式也可能是my @all = split("/n/r/n",$contents);
for(my $i=@all-1;$i>=0;$i--)
{
  print OUT $all[$i],"/n/n";
}
close(IN);
close(OUT);

8. 如何用foreach每次读取多句?
  那就用<FH>读三次,
while (!eof(FH)){
$line1 = <FH>;
$line2 = <FH>;
$line3 = <FH>;
...
}


9 (非)贪婪(non-greedy)匹配

? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,
匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,
而默认的贪婪模式则尽可能多的匹配所搜索的字符串。
例如,对于字符串 "oooo",’o+?’ 将匹配单个"o",而 ’o+’ 将匹配所有 ’o’。

如果你使用一个旧的Perl版本, 并且你不想贪婪匹配, 你必须使用否定的字符类。(真的, 你正在得到一种限制的贪婪匹配)

在Perl的现在版本中, 你能通过在数量词后使用一个问号,
强迫进行非贪婪的最小匹配。 
同样的用户名匹配将是/.*?:/。现在这个.*?将尽可能匹配少的字符, 而不是尽可能多的字符,
所以它停在第一个冒号而不是最后一个冒号。

例子:
问题是这样的:
比如给一句话,形式如下:
I(i)/xx came(come)/xx ,(,)/xx ((()/xx {({)/xx how it like(how it like)/yy
解释格式:
词组,然后是用小括号包围的词组还原形式,然后是斜杠'/',然后是词性..

要求: 把还原形式去掉
例如上句想得到:
I/xx came/xx ,/xx (/xx {/xx how it like/yy

难点: 1. 词组可能是左小括号'(' 即可能有词组:  ((()/xx
      2. 各种标点,括号() [] {}等
      3. 好几个词的词组,如: How it like(how it like)/xx

做法: 拆分成一段一段来做吧,要不然用regexp处理各段之间的混淆很麻烦:

my $str="I(i)/xx came(come)/xx ,(,)/xx ((()/xx )())/x {({)/xx how it like(how it like)/yy";
my $res='';
while($str=~m|(.*?//S+)|g) {# 非贪婪匹配,否则结果就是 I/yy
        my $token=$1;
        $token=~s|/(/(/(/)|/(|g;
        $token=~s|/)/(/)/)|/)|g;
        $token=~s|/(.*/)||g;
        $res.=$token;
}
print $res,"/n";

10 .如何判断某个字符在一个串中出现几次?

$a="abbababababaaadjdsj";
$n=($a=~tr/a//);
print "there are $n a in '$a'/n";

perldoc -q count可以找到

11. perl怎样判断一个数组为空?

_________________________

if (@a){
print "/@a has element!";
}
或者:
unless (@a) {
print "/@a is empty!";
}
____________________
if(scalar @a == 0)

____________________

  @a == 1
  $#a == -1
____________________

12. 统计英文平均句长,字符数

#!
#统计输入文件的平均句长。
#分为两种:
#1.average word length,count by space number
#2.average character length ,count by length() function
#并且要得到full-passage,dlg,essay三种分别统计
use strict;
my $infile = shift;
my $outfile = shift;
open IN,"$infile" or die "can not open inputfile $infile $!/n";
open OUT,">$outfile" or die "can not open outputfile $outfile $!/n";

my $dlg_word_num=0;
my $essay_word_num=0;
my $dlg_chara_num=0;
my$essay_chara_num=0;
my $lines_count=0;
foreach my $line(<IN>){
 $lines_count++;
 my $word_num = ($line=~tr/ / /);
 my $chara_num = length($line);
 $word_num += 1;#词数等于空格数加1
 
 if($lines_count<=437){
  $dlg_word_num+=$word_num;
  $dlg_chara_num+=$chara_num;
 }
 elsif($lines_count<=746){
  $essay_word_num+=$word_num;
  $essay_chara_num+=$chara_num;
  
 }
 else
 {
  print "something wrong with the input file:too many sentence?/n"
  
 }
 
}
print OUT "                avg_word_num            avg_chara_num/n";
print OUT "    dlg:       ",$dlg_word_num/437,"       ",$dlg_chara_num/309," /n";
print OUT "essay:       ",$essay_word_num/437,"    ",$essay_chara_num/309," /n";
print OUT "    all:        ",($dlg_word_num+$essay_word_num)/746,"    ", ($dlg_chara_num+$essay_chara_num)/746;

13. 对数据进行察看

#!
use strict;

require Data::Dumper;
my @trans=("1","asdas",2,3,1.2,"asas");
#print join("/n",@trans);
print Data::Dumper::Dumper(@trans);

14

原创粉丝点击