Perl字符串基本 *** 作详解

Perl字符串基本 *** 作详解,第1张

概述<!-- /* Font Definitions */ @font-face {font-family:"MS Mincho"; panose-1:2 2 6 9 4 2 5 8 3 4; mso-font-alt:"MS 明朝"; mso-font-charset:128; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-fon

<!-- /* Font DeFinitions */ @Font-face {Font-family:"MS Mincho"; panose-1:2 2 6 9 4 2 5 8 3 4; mso-Font-alt:"MS 明朝"; mso-Font-charset:128; mso-generic-Font-family:modern; mso-Font-pitch:fixed; mso-Font-signature:-536870145 1791491579 18 0 131231 0;} @Font-face {Font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-Font-alt:Simsun; mso-Font-charset:134; mso-generic-Font-family:auto; mso-Font-pitch:variable; mso-Font-signature:3 680460288 22 0 262145 0;} @Font-face {Font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-Font-charset:134; mso-generic-Font-family:auto; mso-Font-pitch:variable; mso-Font-signature:3 680460288 22 0 262145 0;} @Font-face {Font-family:"/@MS Mincho"; panose-1:2 2 6 9 4 2 5 8 3 4; mso-Font-charset:128; mso-generic-Font-family:modern; mso-Font-pitch:fixed; mso-Font-signature:-536870145 1791491579 18 0 131231 0;} /* Style DeFinitions */ p.Msonormal,li.Msonormal,div.Msonormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:wIDow-orphan; Font-size:12.0pt; Font-family:"Times New Roman"; mso-fareast-Font-family:宋体;} h1 {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:wIDow-orphan; mso-outline-level:1; Font-size:24.0pt; Font-family:"Times New Roman"; Font-weight:bold;} p {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:wIDow-orphan; Font-size:12.0pt; Font-family:"Times New Roman"; mso-fareast-Font-family:宋体;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->

 

本文和大家重点讨论一下 Perl字符串的一些基本 *** 作,比如 Perl字符串数组元素赋值: @tmp=qw(aaabbbkkk9000);相当于 @tmp=(“aaa”,“bbb”,“kkk”,“9000)。至于其他 *** 作请看本文详细介绍。

Perl 字符串 *** 作

Perl字符串数组元素赋值: @tmp=qw(aaabbbkkk9000);相当于 @tmp=(“aaa”,“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=<STDIN>))
{
for($i=1,$found=0;$i<=$int_num;$i++)
{
if($input==$i){$found=1;}
else
Doyouwanttochangeeth0:2'sipaddress?回车

Argument""isn'tnumericinnumericeq(==)at./address.plline77,<STDIN>line2.
对整形变量 $input==$i,如果 $input是回车,并不走 else,而是报错

正确的做法是:不论整形 Perl字符串,都用 eq
while(chomp($input=<STDIN>))
{
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";
結果變成:
12345大家來跳舞 helloworld

x运算符号
print"OK"x4;
結果變成:
OKOKOKOK


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

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

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

print$v2.$v1;
220
12199

Perl 字符串的连接可以连接整形和字符形,整形也被当作字符型处理,没有 printf 里的 %d 问题
$min=1;

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

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


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


Perl 字符串中 length 取串长 ( 字符数量)
#!/usr/bin/Perl
$str="abCD99e";
$strlen=length($str);
print$strlen,"/n";
[macg@localhostPerltest]$./tip.pl
7

substr 串,位置,长度 ------- 取子串,注意从 0 开始数位置
#!/usr/bin/Perl
$str="ABCDEFG1234567";
$a=substr$str,5;
print$a,"/n";
[macg@localhostPerltest]$./tip.pl
ABCDE

$a=substr$str,-4,2;
从倒数第 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

@ 数组 =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);当一行中各单词间的空格多于一个时


空格和 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);现在才是真正的按空格和 TAB
[macg@localhostPerltest]$./tip.pl
ABCDEi
FG12i

345
6
7
但还是有缺陷, TAB和空格相连时, TAB被认为是空格划分的子串,或者空格被认为是 TAB划分的子串


join 定义 Perl 字符串数组格式符号(缺省是 , )必须与 qw() 合用

语法: join($string,@array)
@array=qw(onetwothree);
$total="one two three";
@array=qw(onetwothree);
$total=join(" ",@array);
$total="one two three";
数组内 grep
@array=("one","on","in");
$count=grep(/on/,@array);
查询结果赋值给单变量
@array=("one","in");
@result=grep(/on/,@array);
查询结果赋值给数组 2 one on

总结

以上是内存溢出为你收集整理的Perl字符串基本 *** 作详解全部内容,希望文章能够帮你解决Perl字符串基本 *** 作详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1290627.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存