免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2079 | 回复: 6
打印 上一主题 下一主题

一个grep疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-14 15:18 |只看该作者 |倒序浏览
昨天看了仙子的简简单单grep,今天做实验发现了一个疑问
code如下:
---------------------------------------------------------------------------------
my @array = qw(200 102 101 115 126 110 112 105 111 121);
my $exist = grep(200,@array);
print "$exist\n";
----------------------------------------------------------------------------------

输出的是10,也就是数组的长度;
然后发现除了将grep中的200用0替换,匹配的结果为0,剩下无论用什么代替,匹配的结果都是10!!
这个现象是怎么回事?

ps:
再有一个小问题; unix调试perl脚本查找排除错误,但要保证脚本不运行应该怎么做?
我写的脚本是操作重要数据库的,要确定没有错误才敢执行,现在的调试工作弄的我心惊肉跳,有没有unix下调试perl的软件呢?

论坛徽章:
0
2 [报告]
发表于 2007-05-14 15:47 |只看该作者
10是匹配的次数
perl -d $0

论坛徽章:
0
3 [报告]
发表于 2007-05-14 15:51 |只看该作者
试试这个

  1. use strict;
  2. use warnings;

  3. my @array = qw(200 102 101 115 126 110 112 105 111 121);
  4. my $exist = grep(/200/,@array);
  5. print $exist;
复制代码

论坛徽章:
0
4 [报告]
发表于 2007-05-14 15:58 |只看该作者
grep BLOCK LIST
grep EXPR,LIST
        This is similar in spirit to, but not the same as, grep(1) and
        its relatives. In particular, it is not limited to using regular
        expressions.

        Evaluates the BLOCK or EXPR for each element of LIST (locally
        setting $_ to each element) and returns the list value
        consisting of those elements for which the expression evaluated
        to true. In scalar context, returns the number of times the
        expression was true.

            @foo = grep(!/^#/, @bar);    # weed out comments

        or equivalently,

            @foo = grep {!/^#/} @bar;    # weed out comments

        Note that $_ is an alias to the list value, so it can be used to
        modify the elements of the LIST. While this is useful and
        supported, it can cause bizarre results if the elements of LIST
        are not variables. Similarly, grep returns aliases into the
        original list, much as a for loop's index variable aliases the
        list elements. That is, modifying an element of a list returned
        by grep (for example, in a "foreach", "map" or another "grep")
        actually modifies the element in the original list. This is
        usually something to be avoided when writing clear code.

论坛徽章:
0
5 [报告]
发表于 2007-05-14 16:24 |只看该作者
多谢alexru 啦
唉,还是我看的不仔细,把//忽略了

论坛徽章:
0
6 [报告]
发表于 2007-05-14 17:04 |只看该作者
perl -d $0  
怎么使用啊,不是要手动一行一行查找错误吧,有没有使用文档呢?

论坛徽章:
0
7 [报告]
发表于 2007-05-15 18:56 |只看该作者

ps:
再有一个小问题; unix调试perl脚本查找排除错误,但要保证脚本不运行应该怎么做?
我写的脚本是操作重要数据库的,要确定没有错误才敢执行,现在的调试工作弄的我心惊肉跳,有没有unix下调试perl的软件呢?

这个用perl -wc就可以了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP