日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Perl字符串基本操作詳解

本文和大家重點討論一下Perl字符串的一些基本操作,比如Perl字符串?dāng)?shù)組元素賦值:@tmp=qw(aaabbbkkk9000);相當(dāng)于@tmp=(“aaa”,“bbb”,“kkk”,“9000)。至于其他操作請看本文詳細介紹。

創(chuàng)新互聯(lián)公司專注于長清網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供長清營銷型網(wǎng)站建設(shè),長清網(wǎng)站制作、長清網(wǎng)頁設(shè)計、長清網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造長清網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供長清網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

Perl字符串操作

Perl字符串?dāng)?shù)組元素賦值:@tmp=qw(aaabbbkkk9000);相當(dāng)于@tmp=(“aaa”,“bbb”,“kkk”,“9000);

◆Perl字符串比較,絕不能用==,要用eq
[macg@localhostPerltest]$vitip.pl

#!/usr/bin/Perl
print"input:";
while(chomp($input=<>)){
print"yourinputis$input\n";
if($input=="q"){print"chooseq\n";last;}
elsif($input=='n'){print"inputis$input\n";next;}
else{print"inputok,tryagain\n";}
print"input:";
}
[macg@localhostPerltest]$./tip.pl
input:x
yourinputisx
chooseq

◆Perl字符串用==是最常犯的錯誤

即使是整形,也盡量用eq,少用==
while(chomp($input=))
{
for($i=1,$found=0;$i<=$int_num;$i++)
{
if($input==$i){$found=1;}
else
Doyouwanttochangeeth0:2'sipaddress?回車

Argument""isn'tnumericinnumericeq(==)at./address.plline77,line2.
對整形變量$input==$i,如果$input是回車,并不走else,而是報錯

正確的做法是:不論整形Perl字符串,都用eq
while(chomp($input=))
{
for($i=1,$found=0;$i<=$int_num;$i++)
{
if($inputeq$i){$found=1;}
}
whichinterfaceyouwanttoconfig?choiceanumber1234q:1
Doyouwanttochangeeth0'sipaddress?

◆Perl字符串幾種連接運算符

運算符,常用于輸出
print"純金",$v1;
print$str,"\n\n";

.運算符和,類似也是Perl字符串相加但,通常只用于print而.可以用在任何Perl字符串相加的地方
print'12345大家來跳舞'."helloworld";
結(jié)果變成:
12345大家來跳舞helloworld

x運算符號
print"OK"x4;
結(jié)果變成:
OKOKOKOK

◆為什么Perl字符串相加只能用.不能用+

因為可能+就是真加了(數(shù)字相加),而不是Perl字符串合并
$v1=99;
$v2='121';

print$v1+$v2;
$v1=99;
$v2='121';

print$v2.$v1;
220
12199

◆Perl字符串的連接可以連接整形和字符形,整形也被當(dāng)作字符型處理,沒有printf里的%d問題
$min=1;

$date="date"."0".$min;
print$date,"\n";

[root@ntrackermac]#./tip.pl
date01

uc轉(zhuǎn)成大寫,lc轉(zhuǎn)成小寫
$str="abCD99e";
$str=uc($str);
$str="abCD99e";
$str=lc($str);
[macg@localhostPerltest]$./tip.pl
ABCD99E
[macg@localhostPerltest]$./tip.pl
abcd99e                    #p#

◆Perl字符串中l(wèi)ength取串長(字符數(shù)量)
#!/usr/bin/Perl
$str="abCD99e";
$strlen=length($str);
print$strlen,"\n";
[macg@localhostPerltest]$./tip.pl
7

◆substr串,位置,長度-------取子串,注意從0開始數(shù)位置
#!/usr/bin/Perl
$str="ABCDEFG1234567";
$a=substr$str,0,5;
print$a,"\n";
[macg@localhostPerltest]$./tip.pl
ABCDE

$a=substr$str,-4,2;
從倒數(shù)第4個開始,取兩個字符
[macg@localhostPerltest]$./tip.pl
45

◆index在字串中找尋某一子字串的起始位置
#!/usr/bin/Perl
$str="ABCDEFG1234567";
$a="12";
$pos=index($str,$a);
print$pos,"\n";
[macg@localhostPerltest]$./tip.pl
7

@數(shù)組=split(pattern,串)將Perl字符串用某模式分成多個單詞
#!/usr/bin/Perl
$str="ABCDEiFG12i34567";
@array=split(//,$str);按空格分
foreach(@array){
print$_,"\n";
}
[macg@localhostPerltest]$./tip.pl
ABCDEi
FG12i
345
6
7

@array=split(/+/,$line);當(dāng)一行中各單詞間的空格多于一個時

◆空格和TAB混雜情況下的split

[macg@localhostPerltest]$vitip.pl

#!/usr/bin/Perl
$str="ABCDEiFG12i34567";
@array=split(/\t/,$str);
foreach(@array){
print$_,"\n";
}
[macg@localhostPerltest]$./tip.pl
ABCDEiFG12i
34567
只分了兩份,為什么?
因為同時滿足TAB和空格的只有一處
所以必須加[]
@array=split(/[\t]/,$str);現(xiàn)在才是真正的按空格和TAB分
[macg@localhostPerltest]$./tip.pl
ABCDEi
FG12i

345
6
7
但還是有缺陷,TAB和空格相連時,TAB被認為是空格劃分的子串,或者空格被認為是TAB劃分的子串

◆用join定義Perl字符串?dāng)?shù)組格式符號(缺省是,)必須與qw()合用

語法:join($string,@array)
@array=qw(onetwothree);
$total="one,two,three";
@array=qw(onetwothree);
$total=join(":",@array);
$total="one:two:three";
數(shù)組內(nèi)grep
@array=("one","on","in");
$count=grep(/on/,@array);
查詢結(jié)果賦值給單變量
@array=("one","on","in");
@result=grep(/on/,@array);
查詢結(jié)果賦值給數(shù)組
2
one
on
 

【編輯推薦】

  1. 解析Perl字符串用法
  2. Perl多線程的兩種實現(xiàn)方式
  3. Perl二維數(shù)組用法全程剖析
  4. Perl基礎(chǔ) Perl 哈希表概述
  5. 解析Perl正則表達式中的模式

名稱欄目:Perl字符串基本操作詳解
標題路徑:http://m.5511xx.com/article/cdpdjds.html