天天看點

Perl語言入門(09 perl中自帶的函數)

由于公司需要,本人最近在學習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是個字元串直接量,它也可以是一個用來描述輸出格式的标量.

Perl語言入門(09 perl中自帶的函數)

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;