由于公司需要,本人最近在学习Perl这种脚本语言,本文是我在学习Perl的过程中总结出来的一些心得和笔记,希望能够帮助也在学习Perl的各位同僚。废话不多说直接上干货!!!
————————————————— 干货分割线 —————————————————
1.index :在另一个标量中搜索单个字符串,从前往后搜索.若没找到目标字符串时返回-1
index string,substring
index string,substring,start_position
i n d e x函数从s t r i n g的左边开始运行,并搜索s u b s t r i n g。i n d e x返回找到s u b s t r i n g时所在的位置,o是指最左边的字符。如果没有找到s u b s t r i n g,i n d e x便返回- 1。
Exp:
index “Ring around the rosy”,”around”; #返回5
index (“Pocket full of posies”,”ket”); #返回3
$a = “Ashes,ashes,we all fall down”;
index($a,”she”); #返回1
index $a,”they”; #返回 -1 (没找到)
@a = qw(oats peas beans);
index join(“”,@a),”peas”; #返回5
Index函数可以添加开始索引:
index($String,$subString,start);
$reindeer = “dasher dancer prancer vixen”;
Index($reindeer ,”da”); #返回0
Index($reindeer ,”da”,1); #从第一个位置开始搜索,返回7
Exp:
my $source = "One fish,two fish,red fish,blue fish";
my $start = -1;
while(($start = index($source,"fish",$start)) != -1){
print "Found a fish at $start\n";
$start++;
}
输出:Found a fish at 4
Found a fish at 13
Found a fish at 22
Found a fish at 32
2.rindex :在另一个标量中搜索单个字符串,从后往前搜索,若没找到目标字符串时返回-1
rindex string,substring
rindex string,substring,start_position
Exp:
$s = “she loves you yeah,yeah,yeash”;
rindex($a,”yeah”); #返回26. 从后往前搜索,查找能第一个匹配的字符串
rindex($a,”yeah”,25); #返回20,从第25个开始往前搜索
$source = “One fish,two fish,red fish,blue fish.”;
$start = length($source);
while(($start = rindex($source,”fish”,$start)) != -1){
print “Found a fish at $start\n”;
$start--;
}
3.Substr函数:s u b s t r函数取出s t r i n g,从位置o ff s e t开始运行,并返回从o ff s e t到结尾的字符串的剩余部分。如果设定了l e n g t h,那么取出l e n g t h指明的字符,或者直到找出字符串的结尾.
Substr string,offset
Substr string,offset,length
Exp:
$a = “I do not like green eggs and ham.”;
Print substr($a,25); #输出”and ham.”
Print substr($a,14,5); #输出”green”;
如果o ff s e t设定为负值, s u b s t r函数将从右边开始计数。例如, s u b s t r($ a , - 5)返回$ a的最后5个字符;如果l e n g t h设定为负值,则s u b s t r返回从它的起点到字符串结尾的值,但不包含最后l e n g t h个字符,如下例所示:
Print substr($a,5,-10); #prints “not like green egg”;从第5个开始,返回字符串的剩余部分,但不包含最后1 0个字符.
当substr函数用在左边时, s u b s t r用于指明标量中的什么字符将被替换。当用在赋值表达式的左边时, s u b s t r的第一个参数必须是个可以赋值的值,如标量变量,而不应该是个字符串直接量。
Exp:
#将第一个字符串用Romans,C替换,
$a = “countrymen.lend me your wallets”;
Substr($a,0,1) = “Romans,C”; #输出:Romans,Countrymen.lend me your wallets
#在字符串开头插入Friends,
Substr($a,0,0) = “Friends, ”;
#以ears.替换最后7个字符
Substr($a,-7,7) = “ears.”; #输出:countrymen.lend me your ears.
4.转换函数tr/searchlist/replacementlist/(也可写成y///):t r / / /用于搜索一个字符串,找出s e a r c h l i s t中的各个元素,并用r e p l a c e m e n t l i s t中的对应元素对它们进行替换。
Tr/ABC/XYZ/; #在$_中,用X替换所有的A,用Y替换所有的B...
$r =~ tr/ABC/XYZ/; #在$r中,用X替换所有的A,用Y替换所有的B..
tr/A-Z/a-z/; #将大写字母转换成小写字母
tr/A-Za-z/a-zA-Z/; #大写转小写,小写转大写
5.Printf函数:
Printf formatstring,list
Printf filehandle formatstring,list
f o r m a t s t r i n g是一个描述输出格式的字符串,l i s t是一个你想让p r i n t f显示的值的列表,它类似p r i n t语句中的l i s t。p r i n t f将它的输出显示给S T D O U T文件句柄,请注意,f i l e h a n d l e名与f o r m a t s t r i n g之间不使用逗号。通常情况下f o r m a t s t r i n g是个字符串直接量,它也可以是一个用来描述输出格式的标量.

Exp:
printf("%20s","jack"); #前面空20个字符
printf("%-20s","Jill"); #后面空20个字符
my $amt = 7.127;
printf("%6.2f",$amt); #共6位数(算.),.后面保留两位并四舍五入
printf("%c",65); #输出字符A
$amt = 9.4;
printf("%6.2f",$amt); #输出:9.40
printf("%6d",$amt); #输出:_,_,_,_,_9; 共6位输出整数
printf("%06.2f",$amt); #输出前导0
s p r i n t f函数与p r i n t f几乎相同,不过它不是输出值,而是输出s p r i n t f返回的格式化输出,可以将它赋予一个标量,或者用于另一个表达式,
Exp:
$weight = 85;
$moonweight = sprintf(“%.2f”,$weight*.17);
print “You weigh $moonweight on the moon.”;
带有% f格式说明符的p r i n t f和s p r i n t f函数能够将计算结果圆整为你指定的小数点位数。
Exp:
#!/usr/bin/perl
use strict;
use warnings;
my @employees = (
'jinping,Xi,123101,9.35,40',
'shimin,Li,139102,12,32,35',
'bodang,Wang,122311,8.88,46',
'shimao,Zhu,132232,12.12,50',
'peisi,Chen,123231,12.00,50'
);
sub print_emp{
my ($last,$first,$emp,$hourly,$time) = split(',',$_[0]);
my $fullname;
$fullname = sprintf("%s %s",$first,$last);
printf("%6d %-20s %6.2f %3d %7.2f\n",
$emp,$fullname,$hourly,$time,
($hourly*$time)+.005);
}
@employees = sort{
my ($L1,$F1) = split(',',$a); #a为源字符串,l1,f1为字符串前两个
my ($L2,$F2) = split(',',$b);
return($L1 cmp $L2 || $F1 cmp $F2); #比较姓氏,若姓氏相同比较名称
} @employees;
foreach(@employees){
print_emp($_);
}
6.堆栈的相关函数:
S h i f t函数用于将元素添加到堆栈的底部,u n s h i f t用于从底部取出元素.
Pop target_array
Shift target_array
Unshift target_array,new_list
Push target_array,new_list
p o p与s h i f t函数分别从t a rg e t a r r a y中删除一个元素。如果t a rg e t a r r a y没有设定,那么元素可以从@ 中删除,也可以从@ A R G V中删除。p o p和s h i f t函数返回被删除的元素,如果数组是空的,则返回u n d e f。数组的大小将相应地缩小。
PS:在子例程中,如果没有设定其他数组,那么p o p、s h i f t、u n s h i f t和p u s h函数将修改@ 。在子例程外面,你的程序主体中,如果没有设定其他数组,那么这些函数将修改数组@ A R G V。
Pop:将数组的最后一个元素取出并返回
@array=5..9;
$fred=pop(@array); #$fred 得到 9,@array 现在为(5,6,7,8)
[email protected]; #$barneygets8,@array 现在为(5,6,7)
[email protected]; #@array 现在为(5,6)(7 被丢弃了)
和 pop 相反的操作是 push,它可以将一个元素(或者一列元素)加在数组的末尾:
push(@array,0); #@array 现在为(5,6,0)
[email protected],8; #@array 现在为(5,6,0,8)
[email protected],1..10; #@array 现在多了 10 个元素
@others=qw/9 0 2 1 0/;
[email protected],@others; #@array 现在又多了 5 个元素(共有 19 个)
push 的第一个参数或者 pop 的唯一参数必须是数组变量。
7.Splice函数:s p l i c e函数用于删除数组中从o ff s e t位置开始的元素,同时返回被删除的数组元素。如果o ff s e t的值是负值,则从数组的结尾处开始计数。如果设定了l e n g t h,那么只删除l e n g t h指定的元素。如果设定了l i s t,则删除l e n g t h指定的元素,并用l i s t的元素取代之。通过这个处理过程,数组就会根据需要扩大或缩小.
Splice array,offset
Splice array,offset,length
Splice array,offset,length,list
Exp:
my @band = qw(trombone ukulele);
splice(@band,0,1); #ukulele
splice(@band,0,0,qw(peas)); #peas ukulele
splice(@band,-1,1,qw(barley turnip)); #peas barley turnip
splice(@band,1,1); #peas turnip
print @band;