My FAQ,最新最全的IT技术FAQ
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 未整理篇 | 技术讨论
  当前位置: > 程序开发 > Shell
map and grep
作者:未知 时间:2005-09-13 23:23 出处:Blog.ChinaUnix.net 责编:My FAQ
              摘要:map and grep
用map函数作用表中各项,用grep函数寻找符合标准的表项.
grep于map的区别在于grep返回表的子表,map则对表里所有元素求值.

1,map函数用法格式:
map BLOCK LIST
map EXPR,LIST
实例:
/home/lee/test#perl -e 'print join(" ",(map lc,ABC,DEF),"\n")'
abc def
用lc函数将表中所有大写字符转成小写

/home/lee/test#perl -e 'print join(" ",(map chr,65 .. 70),"\n")'
A B C D E F
用chr函数返回表中字符码对应的字母,..是区间运算符号,即从哪到哪的一个范围

/home/lee/test#perl -e 'print join(":",(map 1 .. 10),"\n")'
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(1 .. 20)),"\n")'
16 17 18 19 20
打印1到20之间数字,用grep找出大于15的项

/home/lee/test#perl -e 'print join(" ",grep(!/c/,a .. f),"\n")'
a b d e f
打印a到f但是不匹配字符c的各项
perl -e 'print join(" ",(grep$/}(qw(one two three four five))),"\n")'
one two three
打印表中非四个字符的单词
ok,continue,....
怎么blog不能正确显示一些符号?! 郁闷!

 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 myfaq.com.cn All rights reserved. www.myfaq.com.cn 版权所有