大头
Table_bottom

标签云
Table_bottom

分类
Table_bottom

日历
二月
28293031123
45678910
11121314151617
18192021222324
252627282912
Table_bottom

评论
Table_bottom

留言
Table_bottom

微博
Table_bottom

热门文章
Table_bottom

随机文章
Table_bottom

豆瓣上谁关注这里
Table_bottom

链接
Table_bottom

搜索栏
Table_bottom

RSS
RSS Link
Table_bottom

功能
Table_bottom

页面
Table_bottom

计数器
463975
Table_bottom

访客统计
Table_bottom

存档
Table_bottom

初学Perl之四

今天要把提取的字串输出到excel文件中。

一开始是直接写,用'\t'移到下一个单元格,但是不会换行。后来google上搜一搜知道要用Spreadsheet::WriteExcel模块。运行程序报错说“Can't locate Spreadsheet/WriteExcel.pm”,再搜了搜知道要打开PPM(Perl Package Manager)去安装这个模块。用它,写excel文件就方便了。我没有看文档,现在的需求还很简单。

然后碰到下一个问题,写中文乱码。之前搜索的时候看到过很多关于这个问题的帖子,这次是Unicode::Map模块。一样要去PPM安装。一样的方便。

程序如下:

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Spreadsheet::WriteExcel;
  5. use Unicode::Map();
  6.  
  7. my $workbook = Spreadsheet::WriteExcel->new("result.xls");
  8. my $worksheet = $workbook->add_worksheet("hymz");
  9.  
  10. my $Map = new Unicode::Map("GB2312");
  11. my $subset = "书名:</td>";
  12. my $cnt = 0;
  13. my $modflag = 0;
  14.  
  15. while(<>)
  16. {
  17.         if($modflag == 1)
  18.         {
  19.                 #get the book name.
  20.                 $cnt = $cnt + 1;
  21.                 $_ =~ m!>(.*?)</a>!;
  22.                 $worksheet->write($cnt - 1, 0, $cnt);
  23.                 $worksheet->write_unicode($cnt - 1, 1, $Map->to_unicode($1))
  24.         }
  25.  
  26.         my $offset = 0;
  27.         $offset = index($_, $subset);
  28.         if($offset == -1)
  29.         {
  30.                 $modflag = 0;
  31.         }
  32.         else
  33.         {
  34.                 $modflag = 1;
  35.         }
  36. }
  37. $workbook->close();
  38.  

主要参考这篇文章:实例解说:用Perl来分析并生成中文Excel文件

初学Perl之三

今天程序终于跑对了。程序如下:

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $subset = "书名:</td>";
  6. my $modflag = 0;
  7.  
  8. while(<>)
  9. {
  10.         if($modflag == 1)
  11.         {
  12.                 #get the book name.
  13.                 print $1 if $_ =~ />(.*)<\/a>/m;
  14.                 print "\n";
  15.         }
  16.  
  17.         my $offset = 0;
  18.         $offset = index($_, $subset);
  19.         if($offset == -1)
  20.         {
  21.                 $modflag = 0;
  22.         }
  23.         else
  24.         {
  25.                 $modflag = 1;
  26.         }
  27. }

get the book name那里(L13, L14),这样写也可以:

  1.         $_ =~ m!>(.*?)</a>!;
  2.         print $1, "\n";

这样写的时候,'/a'前面加'\'或者不加'\'结果都是对的;而采用第一种写法时不是这样,必须加'\',去掉则不正确,报错信息是:

Bareword found where operator expected at xxxx line 23, near "/>(.*?)</a"

(Missing operator before a?)

syntax error at xxxx line 23, near "/>(.*?)</a"

这是为什么?

初学Perl之二

程序有问题,有关字符串匹配。会用index函数去查找字串,那就能找到正确的行。可是运行总是报错,看不懂报错信息。怀疑是语法错误,却不知道错在哪儿。不像C,编译就排查了语法错误。CSDN上乱翻贴,看到有人推荐一个工具Komodo(下载)。装了后打开我的程序源文件,看到一行波浪线,太好了,这里有问题:

  1. $_ =~ m!>(.*?)</a>;

可是什么问题呢?书上说模式匹配函数(m//)是这样用的:

  1. m/pattern/;

 还说m后的第一个字符是模式分隔符。在一个例子里用!做分隔,我没有正确理解,这是把缺省分隔符(/)换成其他的(!)。因此这样才对:

  1. $_ =~ m!>(.*?)</a>!;

 现在的程序可以跑,但是结果不对。问题还是在这一行。我要取出所需要的字串。哪里不对?应该怎么写?没人可问,继续琢磨。

初学Perl之一

开始学习Perl,起由是想做个小工具,用C也可以,一则不急,二则不忙,那就慢慢做。

先下载ActivePerl,5.10.0 Build 1004。CSDN上看人介绍又下了个perl-lint-mode,也不知道怎么用,先放着。再下载EditPlus做编辑器。

我的第一个perl程序问题是:global symbol requires explicit package name at xxx

google这个报错信息,知道了问题所在。程序里用了use strict之后,就必须用our或者my声明变量。为什么呢?找到了这个介绍:Use Strict And Warnings

程序很简单,我只是想试一下命令行参数。

  1. #!/usr/bin/perl
  2. #use strict;
  3. use warnings;
  4.  
  5. $NUM = @ARGV;
  6. print $NUM;
  7.  
  8. #open(TESTFIEL, "test.txt");
  9. while(<>)
  10. {
  11.         print;
  12. }
  13.