perl 字符串操作
来源:互联网 发布:弗洛伊德算法 c 编辑:程序博客网 时间:2024/05/29 08:38
字符串数组元素赋值:@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=<>)) {
print"your input is $input \n";
if ($input=="q") { print "choose q \n";last;}
elsif ($input=='n') {print "input is $input \n";next;}
else { print "input ok,try again\n";}
print "input:";
}[macg@localhost perltest]$ ./tip.pl
input:x
your input is x
choose q
字符串用==是最常犯的错误
即使是整形,也尽量用eq,少用==
while(chomp($input=<STDIN>))
{
for($i=1,$found=0;$i<=$int_num;$i++)
{
if ($input==$i) { $found=1;}
elseDo you want to change eth0:2 's ip address ? 回车
Argument "" isn't numeric in numeric eq (==) at ./address.pl line 77, <STDIN> line 2.
对整形变量$input==$i,如果$input是回车,并不走else,而是报错
正确的做法是: 不论整形字符串,都用eq
while(chomp($input=<STDIN>))
{
for($i=1,$found=0;$i<=$int_num;$i++)
{
if ($input eq $i) { $found=1;}
}
which interface 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.pl
7
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.pl
45
index 在字串中找尋某一子字串的起始位置
#!/usr/bin/perl
$str = "ABCDEFG1234567";
$a = "12";
$pos=index($str,$a);
print $pos,"\n";[macg@localhost perltest]$ ./tip.pl
7
@数组=split (pattern,串) 将字符串用某模式分成多个单词
#!/usr/bin/perl
$str = "ABCDEi FG12i 345 6 7";
@array=split(/ /,$str);按空格分
foreach (@array) {
print $_,"\n";
} [macg@localhost perltest]$ ./tip.pl
ABCDEi
FG12i
345
6
7
@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) {
print $_,"\n";
}[macg@localhost perltest]$ ./tip.pl
ABCDEi FG12i
345 6 7 只分了两份,为什么?
因为同时满足TAB和空格的只有一处
所以必须加[ ]
@array=split(/[\t ]/,$str); 现在才是真正的按空格和TAB分[macg@localhost perltest]$ ./tip.pl
ABCDEi
FG12i
345
6
7 但还是有缺陷,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
[macg@localhost perltest]$ vi tip.pl
#!/usr/bin/perl
print"input:";
while(chomp($input=<>)) {
print"your input is $input \n";
if ($input=="q") { print "choose q \n";last;}
print "input:";
input:x
your input is x
choose q
while(chomp($input=<STDIN>))
Argument "" isn't numeric in numeric eq (==) at ./address.pl line 77, <STDIN> line 2.
对整形变量$input==$i,如果$input是回车,并不走else,而是报错
while(chomp($input=<STDIN>))
which interface 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 '12345 大家來跳舞' . " hello world";
結果變成:
12345 大家來跳舞 hello world
print "OK" x 4;
結果變成:
OKOKOKOK
因为可能+就是真加了(数字相加),而不是字符串合并
$v1 = 99;
$v2 = '121';
print $v1 + $v2;$v1 = 99;
$v2 = '121';
print $v2 . $v1;22012199
$min=1;
$date="date "."0".$min;
print $date,"\n";
date 01
$str="abCD99e";
$str = uc($str);$str="abCD99e";
$str = lc($str);[macg@localhost perltest]$ ./tip.pl
ABCD99E
abcd99e
#!/usr/bin/perl
$str="abCD99e";
$strlen=length($str);
print $strlen,"\n";[macg@localhost perltest]$ ./tip.pl
7
#!/usr/bin/perl
$str = "ABCDEFG1234567";
$a = substr $str, 0, 5;
print $a,"\n";[macg@localhost perltest]$ ./tip.pl
ABCDE
$a = substr $str, -4, 2;
45
#!/usr/bin/perl
$str = "ABCDEFG1234567";
$a = "12";
$pos=index($str,$a);
print $pos,"\n";[macg@localhost perltest]$ ./tip.pl
7
#!/usr/bin/perl
$str = "ABCDEi FG12i 345 6 7";
@array=split(/ /,$str);按空格分
foreach (@array) {
}
ABCDEi
FG12i
345
6
7
@array = split (/ +/, $line);
[macg@localhost perltest]$ vi tip.pl
#!/usr/bin/perl
$str = "ABCDEi FG12i
@array=split(/\t /,$str);
foreach (@array) {
}[macg@localhost perltest]$ ./tip.pl
ABCDEi FG12i
345 6 7
因为同时满足TAB和空格的只有一处
所以必须加[ ]
@array=split(/[\t ]/,$str);
ABCDEi
FG12i
345
6
7
语法:join($string,@array)
@array=qw(one two three);$total="one,two,three";@array=qw(one two three);
$total=join(":",@array);
@array=("one","on","in");
$count =grep(/on/,@array);
查询结果赋值给单变量@array=("one","on","in");
@result=grep(/on/,@array);
查询结果赋值给数组2one
on
- perl 字符串操作
- Perl的字符串操作
- perl 字符串操作
- perl 字符串基本操作
- perl 字符串相关操作
- perl字符串操作符
- Perl字符串基本操作详解
- Perl字符串基本操作详解
- Perl 字符串操作 以及 自定义排序
- Perl字符串
- perl里字符串取长度,比较等一些操作
- Perl学习笔记(9) 字符串操作与排序
- 用perl对字符串进行全角转半角操作
- Perl中的字符串
- perl字符串处理函数
- Perl 生成随机字符串
- Perl-02---字符串
- Perl-04---字符串函数
- perl关联数组/哈希表
- FAT文件系统学习记录一
- POJ 1486 唯一二分图匹配 好题
- android模拟器发布APK
- svn 命令行 批量删除和批量添加
- perl 字符串操作
- PHP应该注意的一些常要注意但是又常没有做的配置
- FAT文件系统学习记录二
- perl foreach用法
- 如何在SSH环境下使用纯JDBC操作数据库
- perl sort函数的用法
- openssl实例
- perl 中my 和 our 的区别
- Java 类中的继承、实现、依赖、关联、聚合、组合