perl 字串操作

2021-05-23 07:44:51 字數 3777 閱讀 6895

字串陣列元素賦值:@tmp=qw(aaa bbb kkk 9000);相當於@tmp= (「aaa」, 「bbb」,「kkk」,「9000);

字串比較,絕不能用==  ,要用eq

[macg@localhost perltest]$ vi tip.pl

#!/usr/bin/perl

print"input:";

while(chomp($input=<>))

elsif ($input=='n')

else

print "input:";

}[macg@localhost perltest]$ ./tip.pl

input:x

your input is x

choose q

字串用==是最常犯的錯誤

即使是整形,也盡量用eq,少用==

while(chomp($input=))

else

do you want to change eth0:2 's ip address ? 回車

argument "" isn't numeric in numeric eq (==) at ./address.pl line 77, line 2.

對整形變數$input==$i,如果$input是回車,並不走else,而是報錯

正確的做法是:    不論整形字串,都用eq

while(chomp($input=))

}which inte***ce you want to config ? choice a number 1 2 3 4 q:1

do you want to change eth0 's ip address ?

字串幾種連線運算子

,運算子,常用於輸出

print "純金 ", $v1;

print $str, "/n/n";

.運算子

和,類似也是字串相加

但,通常只用於print 而.可以用在任何字串相加的地方

print '12345 大家來跳舞' . " hello world";

結果變成:

12345 大家來跳舞 hello world

x運算符號

print "ok" x 4;

結果變成:

okokokok

為什麼字串相加只能用.  不能用+

因為可能+就是真加了(數字相加),而不是字串合併

$v1 = 99;

$v2 = '121';

print $v1 + $v2;

$v1 = 99;

$v2 = '121';

print $v2 . $v1;

22012199

字串的連線可以連線整形和字元形,整形也被當作字元型處理,沒有printf裡的%d問題

$min=1;

$date="date "."0".$min;

print $date,"/n";

[root@ntracker mac]# ./tip.pl

date 01

uc   轉成大寫, lc轉成小寫

$str="abcd99e";

$str = uc($str);

$str="abcd99e";

$str = lc($str);

[macg@localhost perltest]$ ./tip.pl

abcd99e    

[macg@localhost perltest]$ ./tip.pl

abcd99e

length取串長(字元數量)

#!/usr/bin/perl

$str="abcd99e";

$strlen=length($str);

print $strlen,"/n";

[macg@localhost perltest]$ ./tip.pl7

substr  串,位置,長度      -------  取子串,注意從0開始數字置

#!/usr/bin/perl

$str = "abcdefg1234567";

$a = substr $str, 0, 5;

print $a,"/n";

[macg@localhost perltest]$ ./tip.pl

abcde  

$a = substr $str, -4, 2; 

從倒數第4個開始,取兩個字元

[macg@localhost perltest]$ ./tip.pl45

index         在字串中找尋某一子字串的起始位置

#!/usr/bin/perl

$str = "abcdefg1234567";

$a = "12";             

$pos=index($str,$a);

print $pos,"/n";

[macg@localhost perltest]$ ./tip.pl7

@陣列=split (pattern,串)          將字串用某模式分成多個單詞

#!/usr/bin/perl

$str = "abcdei fg12i 345 6 7";

@array=split(/ /,$str);按空格分

foreach (@array)    

[macg@localhost perltest]$ ./tip.pl

abcdei

fg12i

34567

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

空格和tab混雜情況下的 split

[macg@localhost perltest]$ vi tip.pl

#!/usr/bin/perl

$str = "abcdei fg12i     345 6 7";

@array=split(//t /,$str);

foreach (@array)

[macg@localhost perltest]$ ./tip.pl

abcdei fg12i

345 6 7

只分了兩份,為什麼?

因為同時滿足tab和空格的只有一處

所以必須加[ ]

@array=split(/[/t ]/,$str);     現在才是真正的按空格和tab分

[macg@localhost perltest]$ ./tip.pl

abcdei

fg12i

34567

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

用join定義字串陣列格式符號(預設是,)  必須與qw( )合用

語法:join($string,@array)

@array=qw(one two three);

$total="one,two,three";

@array=qw(one two three);

$total=join(":",@array); 

$total="one:two:three";

陣列內grep

@array=("one","on","in");

$count =grep(/on/,@array);

查詢結果賦值給單變數

@array=("one","on","in");

@result=grep(/on/,@array);

查詢結果賦值給陣列

2one

on

perl 字串基本操作

1 perl字串中length取串長 字元數量 usr bin perl str abcd99e strlen length str print strlen,n macg localhostperltest tip.pl 72 substr串,位置,長度 取子串,注意從0開始數字置 usr bin...

Perl字串基本操作詳解

本文和大家重點討論一下 perl字串的一些基本操作,比如 perl字串陣列元素賦值 tmp qw aaabbbkkk9000 相當於 tmp aaa bbb kkk 9000 至於其他操作請看本文詳細介紹。perl字串操作 perl字串陣列元素賦值 tmp qw aaabbbkkk9000 相當於 ...

Perl讀寫檔案 字串操作

perl中讀寫檔案的方法非常簡單,可以使用open或sysopen函式來開啟檔案,linux下執行perl指令碼只需 xx.pl 或 perl xx.pl。讀檔案 open 檔案控制代碼,檔名 或者 open 檔案控制代碼,檔名 如 open in,test.txt while close in 寫...