map and grep
时间:2005-04-21 来源:寂寞烈火
grep于map的区别在于grep返回表的子表,map则对表里所有元素求值.
1,map函数用法格式:
map BLOCK LIST
map EXPR,LIST
实例:
/home/lee/test#perl -e 'print join(" ",(map lc,ABC,DEF)," ")'
abc def
用lc函数将表中所有大写字符转成小写
/home/lee/test#perl -e 'print join(" ",(map chr,65 .. 70)," ")'
A B C D E F
用chr函数返回表中字符码对应的字母,..是区间运算符号,即从哪到哪的一个范围
/home/lee/test#perl -e 'print join(":",(map {2*$_} 1 .. 10)," ")'
2:4:6:8:10:12:14:16:18:20:
将表中的所有数字乘2,$_变量是perl的默认变量
2,grep函数用法格式:
grep BLOCK LIST
grep EXPR,LIST
实例练习:
/home/lee/test#perl -e 'print join(" ",(grep{$_>15}(1 .. 20))," ")'
16 17 18 19 20
打印1到20之间数字,用grep找出大于15的项
/home/lee/test#perl -e 'print join(" ",grep(!/c/,a .. f)," ")'
a b d e f
打印a到f但是不匹配字符c的各项
perl -e 'print join(" ",(grep{!/^w{4}$/}(qw(one two three four five)))," ")'
one two three
打印表中非四个字符的单词
ok,continue,....
怎么blog不能正确显示一些符号?! 郁闷!