map 就是针对列表的foreach操作,可带块和正则之一。列表的项目当成临时的$_,块的最后一个返回值,会push到目标列表/散列(array/hash)。其中,使用到的$_和push的目标,都是临时的,不影响外部变量。 <- 这是我理解的。希望别人纠正。map的中文说明,太少了。英文的说明,搜索到的,都看不懂。
代码: 全选
● g map cairo2png.pl
@bg=map /=>\s*background\s+(.*)/,@line;
print "\n===> find background lines\n"; print map "$_\n",@bg;
@bg=map {split /\s+/} @bg; # 拆分多文件的行
@bg=map {-d $_?glob "$_/*.png":$_} @bg; # 扩展目录成可用图片文件
@append=map /^(.{1,60})/s,@append;
代码: 全选
@bg=map /=>\s*background\s+(.*)/,@line;
代码: 全选
print map "$_\n",@bg;
代码: 全选
@bg=map {split /\s+/} @bg;
代码: 全选
@bg=map {-d $_?glob "$_/*.png":$_} @bg;
代码: 全选
@append=map /^(.{1,60})/s,@append;
附带说下grep。额,带了注释的。如果懂了一半的map。下面的应该不需要说了。符合条件的才返回嘛。
代码: 全选
● g grep cairo2png.pl
@line=grep !m"^//",@line; # 去掉注释的行
@bg=grep {-f && /\.png$/} @bg; # 选择有效的png文件
@line=grep !/background/,@line; # 去掉背景设置的行
{@append=grep !/^\s*$/,`cat $s`;
代码: 全选
@info=`mocp -i`;
%hinfo=map{split /: /} @info;
print $hinfo{Title};
昨天,还发现,痛了几天的上火牙齿,用人工牛黄甲硝唑胶囊和复方牙痛酊,居然搞定了。
代码: 全选
g grep bin/cairo-weather.pl
8:open RC,"<$rc"; @rc=grep ! /^\s*#/ && ! /^\s*$/,<RC>; close RC;
47:@alllunar=grep {! /\d{4}/ || /2011/} `/usr/bin/calendar -A $max`;
86:@lunar=grep /$m月.*$d \t/,@alllunar;