1. tr 转换 转换不是替换(tr///==y///)

tr/searchlist/replacementlist/用于查找某个一个字符串,并用replacementlist替换,可以使用正则表达式

my $str="this Is A teST";

$str=~tr/a-z/A-Z/; 把小写转换为大写 会输出THIS IS A TEST

s///  options

g 全局替换

i 忽略大小写

my $str3="this is a test\n";

$str3=~s/t/haha/g;                  =====>hahahis is a hahaeshaha
print $str3;
$str3=~s/HaHa/t/ig;                 =====>this is a test
print $str3;

this is a test

tr 与 替换的区别

tr可以使用正则表达式,替换不行,没法使用$str=~s/a-z/A-Z/; 不会有任何改变 tr/ab/ABC/ 替换原则是a->A b->B   而s/ab/ABC/会将ab->ABC

  • tr///

    The transliteration operator. Same as y///.

$count  = $str3=~ tr/i/i/; # count the stars in $sky   count  the number of a and store into $count

统计i的个数存放在$count

#!/usr/bin/perl
use strict;
my $str1="this Is A tr TEST";
my $str2="this Is A tr TEST";
$str1=~s/t/T/g;
$str2=~tr/a-z/A-Z/;
print "$str1\n";
print "$str2\n";
my $str3="this is a test\n";
my $count=$str3=~tr/i/i/;
print "\ncount the number of i --->$count<----\n";

结果:

D:\>perl tr.pl
This Is A Tr TEST
THIS IS A TR TEST

count the number of i --->2<----

tr  Options:

c Complement the SEARCHLIST.                   清单没写到的就补给他右边清单的最后一个字元
d Delete found but unreplaced characters.      对照表中没有的项目就删掉 
s Squash duplicate replaced characters.        重复的字符变成一个
 

my $strs="have a good day\n";
$strs=~tr/a/_/c;                      =====>  _a___a_______a__

print $strs;

my $strs="have a good day\n";

$strs=~tr/ao/Ao/s;    =====>hAve A god dAy 两个O变成一个
 

my $strs="have a good day\n";
$strs=~tr/hag/HA/d;                 ===>HAve A ood dAy

参考:http://www.cnblogs.com/blueicely/archive/2012/12/13/2816371.html

生成随机数:

rand

Returns a random fractional number greater than or equal to 0 and less than the value of EXPR.

example:

int(rand(10))

returns a random integer between 0 and 9, inclusive.

perl的一些函数(二)的更多相关文章

  1. 【Perl学习笔记】1.perl的ref 函数

    perl有引用的概念:一组数据实际上是另一组数据的引用.这些引用称为指针,第一组数据中存放的是第二组数据的头地址.引用的方式被用得相当普遍,特别是在面向对象的模块.函数的参数传递等常见.但perl对每 ...

  2. perl的map函数

    perl的map函数的使用: 语法 map EXPR, LIST map BLOCK LIST 定义和使用 对list中的每个元素执行EXPR或BLOCK,返回新的list.对每一此迭代,$_中保存了 ...

  3. python之内置函数(二)与匿名函数、递归函数初识

    一.内置函数(二)1.和数据结构相关(24)列表和元祖(2)list:将一个可迭代对象转化成列表(如果是字典,默认将key作为列表的元素).tuple:将一个可迭代对象转化成元组(如果是字典,默认将k ...

  4. c/c++ 图相关的函数(二维数组法)

    c/c++ 图相关的函数(二维数组法) 遍历图 插入顶点 添加顶点间的线 删除顶点 删除顶点间的线 摧毁图 取得与v顶点有连线的第一个顶点 取得与v1顶点,v1顶点之后的v2顶点的之后的有连线的第一个 ...

  5. python---day14( 内置函数二)

    内置函数二一:匿名函数 lambda函数 lambda 表示匿名函数,不需要用def 来申明. 语法: 函数名=lambda 参数:返回值 ----〉 案例:f=lambda n:n*n 例子01: ...

  6. 一款多功能的移动端滚动选择器,支持单选到多选、支持多级级联、提供自定义回调函数、提供update函数二次渲染、重定位函数、兼容pc端拖拽等等..

    https://github.com/onlyhom/mobileSelect.js/blob/master/docs/README-CN.md mobileSelect.js 一款多功能的移动端滚动 ...

  7. C#中的函数(二) 有参有返回值的函数

    接上一篇 C#中的函数(-) 无参无返回值的函数 http://www.cnblogs.com/fzxiaoyi/p/8502613.html 这次研究下C#中的函数(二) 有参有返回值的函数 依然写 ...

  8. python函数知识六 内置函数二、匿名函数与内置函数三(重要)

    19.内置函数二 abs():绝对值 lst = [1,2,-3,1,2,-5] print([abs(i) for i in lst]) enumerate("可迭代对象",&q ...

  9. Perl字符串处理函数用法集锦

    Perl字符串处理函数 0.函数名 index 调用语法position=index(string,substring,position); 解说返回子串substring在字符串string中的位置 ...

  10. perl中sprintf函数的用法

    对于某些字符串,需要输入为特定的格式,通过sprintf可以很方便的完成,不需要专门进行其他处理. 转载 perl中sprintf函数的使用方法.

随机推荐

  1. 15分钟学会使用Git和远程代码库

    git是个了不起但却复杂的源代码管理系统.它能支持复杂的任务,却因此经常被认为太过复杂而不适用于简单的日常工作.让我们诚实一记吧:Git是复杂的,我们不要装作它不是.但我仍然会试图教会你用(我的)基本 ...

  2. vim查找替换

    http://www.cnblogs.com/ltang/articles/2034291.html %: 表示百分百,表示所有 行的意思... 如果不指定行号, 则表示当前行; g: 表示一行中的所 ...

  3. python 运行时报错误SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2

    File "1.py", line 2SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2, but no ...

  4. R笔记1

    gsub format > measurements<-c('3.95*3.99*2.43mm','3*3*5mm','2*2*2mm') > measurements [1] &q ...

  5. POJ 2452 Sticks Problem

    RMQ+二分....枚举 i  ,找比 i 小的第一个元素,再找之间的第一个最大元素.....                   Sticks Problem Time Limit: 6000MS ...

  6. js改变HTML元素的值

    js改变HTML元素的值(常用,备忘) <!DOCTYPE html> <html> <body> <h1>我的第一段 JavaScript</h ...

  7. PHP跳转页面的几种实现方法详解

    •PHP页面跳转一.header()函数header()函数是PHP中进行页面跳转的一种十分简单的方法.header()函数的主要功能是将HTTP协议标头(header)输出到浏览器.header() ...

  8. netbeans tomcat

    http://www.cnblogs.com/fengzy/archive/2013/05/18/3086371.html http://blog.csdn.net/xumengxing/articl ...

  9. 学C++50条建议

    1.把C++当成一门新的语言学习(和C没啥关系!真的.): 2.看<Thinking In C++>,不要看<C++变成死相>: 3.看<The C++ Programm ...

  10. Nginx初学者指南

    Starting, Stopping, and Reloading Configuration To start nginx, run the executable file. Once nginx ...