perl map
来源:互联网 发布:sas mac版安装教程 编辑:程序博客网 时间:2024/06/06 02:22
perl的map函数
perl的map函数的使用:
语法
map EXPR, LIST
map BLOCK LIST
定义和使用
对list中的每个元素执行EXPR或BLOCK,返回新的list。对每一此迭代,$_中保存了当前迭代的元素的值。
返回值
如果返回值存储在scalar标量中,则代表map()返回数组的元素个数;
如果返回值存储在list中,则代表map()函数的数组;
实例1 (将单词首字母大写)
@myNames = ('jacob', 'alexander', 'ethan', 'andrew');
@ucNames = map(ucfirst, @myNames);
$numofucNames = map(ucfirst, @myNames);
foreach $key ( @ucNames ){
print "$key\n";
}
print $numofucNames;
结果为
Jacob
Alexander
Ethan
Andrew
4
实例2 (获得所有的书名中包含的单词,且转化为大写)
my@words = map{split(/\s+/,$_)}@books;
my@uppercases = map uc,@words;
foreach $upword ( @uppercases ){
print "$upword\n";
}
结果为 (Perl map函数的输入数组和输出数组不一定等长,在split起过作用之后,当然@words的长度要比@books长了。)
PRIDEAND
PREJUDICE
EMMA
MASFIELD
PARK
SENSEAND
SENSIBILITY
NOTHANGER
ABBEY
PERSUASION
LADY
SUSAN
SANDITON
THE
WATSONS
实例3 (将多余2位的数字提取到新的list)
my @buildnums = ('R010','T230','W11','F56','dd1');
my @nums = map{/(\d{2,})/} @buildnums;
foreach $num (@nums){
print "$num \n"
}
$a = 'RRR3ttt';
@yy = $a=~/RRR.*ttt/;
$numofyy = $a=~/RRR.*ttt/;
print "@yy\n";
print "$numofyy\n" ;
@yy2 = $a=~/(RRR).*(ttt)/;
$numofyy2 = $a=~/(RRR).*(ttt)/;
print "@yy2\n";
print "$numofyy2\n";
print "$1 $2";
结果为 (正则表达式匹配后返回的为数组或长度,取决于表达式中是否有()或者接收的变量类型)
010
230
11
56
1
1
RRR ttt
1
RRR ttt
完!
感谢,Thanks!
- perl map
- Perl: map print
- perl map函数使用
- perl map函数学习
- perl的map函数
- perl: grep, map
- perl的map函数
- perl map&grep
- perl的map函数
- perl map函数
- perl的map函数
- perl map函数
- perl方法:简简单单讲map
- Perl中的grep和map
- perl docs:简简单单讲map
- perl map 和grep使用
- Perl中的map和grep
- 打卡9:perl grep/map
- Tomcat Manager 密码问题
- Sybase ASE12.5数据库状态值的含义
- java利用反射实例化类、使用类中普通方法、使用类main(静态)方法
- splite
- TestRecoder For WatiN 重构与优化手记
- perl map
- java concurrent 探秘(经典的线程个数,是否完成等的控制)
- 新买的HTC G3 进入fastboot 模式
- debian6开机启动脚本的方法
- 优秀的软件企业为何倒下?
- 读书笔记 <Bootstrap Yourself with Linux USB Stack>
- Cocoachina教学:Core Animation入门
- 项目经理的工具箱---走出软件作坊:三五个人十来条枪 如何成为开发正规军
- Android 资料整理