Perl Study Log (持续更新中)
发布时间:2020-12-16 00:33:40 所属栏目:大数据 来源:网络整理
导读:Perl Study Note You can also look at : http://docs.google.com/View?id=df5hdrsv_198hst39ndw ? Environment Setup --Free Envrionment http://www.activestate.com/activeperl/ ? Run perl program: Windows console: Perl hello.pl ? ? Perl Function i
Perl Study Note
You can also look at :
http://docs.google.com/View?id=df5hdrsv_198hst39ndw
?
Environment Setup
--Free Envrionment
http://www.activestate.com/activeperl/
?
Run perl program:
Windows console: Perl hello.pl
?
?
Perl Function in common use
函数大全:
http://www.mypcera.com/book/7/cgi/0007.htm
Model match function
1. split $line = "I am a new student"; @array = split(/ /,$line); //把$line按照模式匹配分解后依次存入数组 File function 1. seek? //设置文件的当前位置!当一个文件非常大时可以从指定位置读起 seek FILEHANDLE,POSITION,WHENCE? ?成功返回真,失败返回假。 POSITION?是读入的新位置(字节)。 WHENCE? ?有3个值,0表示新位置是POSITION,1表示当前位置加上POSITION,2表示文件尾加上POSITION Array Function (1) sort--按字符顺序排序 ????@array = ("this","is","a","test"); ????@array2 = sort(@array); # @array2 = ("a","test","this") ????@array = (70,100,8); ????@array = sort(@array); # @array = (100,70,8) now ( 2) reverse--反转数组 ????@array2 = reverse(@array); ????@array2 = reverse sort (@array); (3) chop--数组去尾 ????chop的意义是去掉STDIN(键盘)输入字符串时最后一个字符--换行符。而如果它作用到数组上,则将数组中每一个元素都做如此处理。 ????@list = ("rabbit","12345","quartz"); ????chop (@list); # @list = ("rabbi","1234","quart") now ( 4) join/ split--连接/拆分 ????join的第一个参数是连接所用的中间字符,其余则为待连接的字符数组。 ????$string = join(" ","this","string"); # 结果为"this is a string" ????@list = ("words","and"); ????$string = join("::",@list,"colons"); #结果为"words::and::colons" ????@array = split(/::/,$string); # @array = ("words","and","colons") now Perl ?Grammar参考网站? http://www.cbi.pku.edu.cn/chinese/documents/perl/index.htm1. variable
$string = xxxx;
?
2. Array
@array = (1,2,3);
?
print (@array); // 结果为:123
print ("@array"); //结果为:1 2 3 $para = @array; //结果为3,para获取的是array的数组元素个数 ($para) = @array; //结果为1, para获取的是array的第一个元素值
?
/@array;?? #@array的地址
%myarray; #%array是关联数组 3. File R&W
Open?
Test file status:open(FILE,"testFile.txt"); //拥有返回值 open(FILE,"test.txt")||die("open file failed"); //如果打开失败显示失败信息 Read $line = <FILE>; //获取文件的一行,文件指针跳到该行行末 @array = <FILE>; //文件的每一行连同回车符成为数组的一个元素值 Write: open (OUTFILE,">testFile.txt"); // >代表新内容覆盖该文件 open (OUTFILE,">>testFile.txt"); // >>代表新内容从文件尾继续添加 print FILE ("new text added!"); -option expr: ex.??? (-e "testfile.txt")||die("file not existed!");
???????????????????? 文件测试操作符
命令行参数 @argv 用于接收命令行参数, 如c语言的argc argv ex.? Perl testperl.pl para1 para2 //这里是执行脚本,并输入两个参数 在文本中只要使用 @argv就可以得到命令行输入的两个参数 print ("@argv"); //结果为: para1 para2 4. Model Match/def/ 就是模式匹配的基本语法,e.g$line = "aabbccdd xia tao eeffgg"; if($line =~ /xia tao/) //如果$line中有匹配”xia tao“的就返回true,否则false { ??? print("Yes!"); } =~ 和!~是两种描述方式,结果相反. 模式中的特殊字符 1. + +意味着一个或多个相同的字符,如:/de+f/指def、deef、deeeeef等。它尽量匹配尽可能多的相同字符,如/ab+/在字符串abbc中匹配的将是abb,而不是ab。 2.? 字符 []和[^] []意味着匹配一组字符中的一个,如/a[0123456789]c/将匹配a加数字加c的字符串。与+联合使用例:/d[eE]+f/匹配def、 dEf、deef、dEdf、dEEEeeeEef等。^表示除其之外的所有字符,如:/d[^eE]f/匹配d加非e字符加f的字符串。 3、字符 *和?? ?? 它们与+类似,区别在于*匹配0个、1个或多个相同字符,?匹配0个或1个该字符。如/de*f/匹配df、def、deeeef等;/de?f/匹配df或def。 4、转义字符 ?? 如果你想在模式中包含通常被看作特殊意义的字符,须在其前加斜线""。如:/*+/中*即表示字符*,而不是上面提到的一个或多个字符的含义。斜线的表示为//。 5、匹配任意字母或数字 ?? 上面提到模式/a[0123456789]c/匹配字母a加任意数字加c的字符串,另一种表示方法为:/a[0-9]c/,类似的,[a-z]表示任意小写字母,[A-Z]表示任意大写字母。任意大小写字母、数字的表示方法为:/[0-9a-zA-Z]/。 6、字符范围转义
?? 例:/[da-z]/匹配任意数字或小写字母。 更多细节参看? http://www.cbi.pku.edu.cn/chinese/documents/perl/perl6.htm 替换操作符 语法为 s/pattern/replacement/,其效果为将字符串中与pattern匹配的部分换成replacement。如: ???? $string = "abc123def"; ???? $string =~ s/123/456/; # now $string = "abc456def"; 5. Commentsperl的注释有两种, 单行注释 #多行注释 以 =开头,加文字, 以=cut结尾 e.g =comments begin $s = "content"; ... ... =cut 其中, 第一个=号后面可以任意写 6. Condition control一、条件判断 ? ?if?( <expression>) { ????<statement_block_1> ??} ??elsif ( <expression> ) { ????<statement_block_2> ??} ??... ??else{ ????<statement_block_3> ??} 二、循环: 1、 while循环 ??while ( <expression> ) { ????<statement_block> ??} 2、 until循环 ??until ( <expression> ) { ????<statement_block> ??} 3、 类C的for循环?,如 ??for ($count=1; $count <= 5; $count++) { ????# statements inside the loop go here ??} 下面是在for循环中使用逗号操作符的例子: ??for ($line = <STDIN>,$count = 1; $count <= 3;?? $line = <STDIN>,$count++) { ????print ($line); ??} 它等价于下列语句: ??$line = <STDIN>; ??$count = 1; ??while ($count <= 3) {? ????print ($line); ????$line = <STDIN>; ????$count++; ??} 4、针对列表(数组)每个元素的循环: foreach, 语法为: ??foreach localvar (listexpr) { ????statement_block; ??} 例: ??foreach $word (@words) { ????if ($word eq "the") { ??????print ("found the word 'the'n");? ????} ??} 注: (1)此处的循环变量localvar是个局部变量,如果在此之前它已有值, 则循环后仍恢复该值。 (2) 在循环中改变局部变量,相应的数组变量也会改变,如: ??@list = (1,3,4,5); ??foreach $temp (@list) { ????if ($temp == 2) { ??????$temp = 20; ????} ??} 此时@list已变成了(1,20,5)。 5、do循环 ??do { ????statement_block ??} while_or_until (condexpr); ??do循环至少执行一次循环。 6、循环控制 ??退出循环为last,与C中的break作用相同;执行下一个循环为next,与C中的continue作用相同;PERL特有的一个命令是redo,其含义是重复此次循环,即循环变量不变,回到循环起始点,但要注意,redo命令在do循环中不起作用。 7、传统的goto label;语句。
?
7. 字符串字符串联结和重复操作符 联接: . 重复:x 联接且赋值(类似+=): .= 例: $newstring = "potato" . "head"; $newstring = "t" x 5; $a = "be"; $a .= "witched"; # $a is now "bewitched" 符串联结和重复操作符
联接: .
7. 操作剪贴板use Win32::Clipboard;$CLIP = Win32::Clipboard(); print "Clipboard contains: ",$CLIP->Get(),"n"; $CLIP->Set("some text to copy into the clipboard"); $CLIP->Empty(); $CLIP->WaitForChange(); http://rubyforge.org/docman/view.php/85/1694/README.html http://waterlin.blog.35.cn/articles/%E7%94%A8win32clipboard%E6%A8%A1%E5%9D%97%E6%9D%A5%E6%93%8D%E4%BD%9Cwindows%E7%9A%84%E5%89%AA%E5%88%87%E6%9D%BF.html 8. 读文件夹内容
chdir ("./");
my @dircontents;
#Parse Summary number
@dircontents = split(/s/,`dir`); ?
foreach (@dircontents)?
{
? if (/.txt$/i) {
? $log_name =$_;
print ("Get log Name:$log_name^^n");
? }
}
use strict my
http://hlxcx.blog.ccidnet.com/blog-htm-do-showone-uid-85725-type-blog-itemid-326226.html
Spreadsheet
http://aspn.activestate.com/ASPN/CodeDoc/Spreadsheet-WriteExcel/WriteExcel.html#FORMULAS%20AND%20FUNCTIONS%20IN%20EXCEL
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读