免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2916 | 回复: 9

求解《Intermediate Perl》上一道习题! [复制链接]

论坛徽章:
0
发表于 2008-06-19 11:09 |显示全部楼层
Write a program that takes a list of filenames on the command line and uses grep to select the ones whose size in bytes is less than 1000. Use map to transform the strings in this list, putting four space characters in front of each and a newline character after. Print the resulting list.

书上提供的答案:
print map { "    $_\n" } grep { -s < 1000 } @ARGV;


我在WIN平台运行总出现错误:
Warning: Use of "-s" without parentheses is ambiguous at oo.pl.txt line 1.
Unterminated <> operator at oo.pl.txt line 1.


-s需要圆括号么?貌似不用的。请大家指教,谢谢!

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2008-06-19 15:38 |显示全部楼层
在windows环境下-s无效...
另外
-x 判断文档是否可以执行
-g/-k/-l/-p/-u/-A 不具意义
应该是因为没有inode吧....
我这本书的版本可能是5.6里面的..5.10不知道activeperl有没有改进...

[ 本帖最后由 apile 于 2008-6-19 15:53 编辑 ]

论坛徽章:
0
发表于 2008-06-19 17:04 |显示全部楼层
Apile大哥:我WIN XP上perl版本为: 5.8.8
我需要到哪里查看WIN上使用Perl与Unix上使用的差别?容易混淆......谢谢!

论坛徽章:
0
发表于 2008-06-19 18:09 |显示全部楼层

回复 #1 windows.li 的帖子

under Windows, using "lt" operand  instead of "<" !
Try this:
cd C:\windows
C:\windows>perl -e "print map{qq($_\n)} grep{-s lt 2000} glob('*.bmp')"

论坛徽章:
0
发表于 2008-06-20 10:22 |显示全部楼层
谢谢两位大哥的解答!

论坛徽章:
0
发表于 2012-08-10 16:10 |显示全部楼层
本帖最后由 mouse.rice 于 2012-08-13 12:07 编辑

回复 4# ulmer


为何改为如下不行呢?
  1. perl -e qq/print map {"$_\n"} grep {-s lt 2000} glob('*.bmp')/;
复制代码

论坛徽章:
0
发表于 2012-08-10 17:28 |显示全部楼层
本帖最后由 sjdy521 于 2012-08-10 17:29 编辑

数字比较也用lt。。怎么想的
  1. Use of "-s" without parentheses is ambiguous
复制代码
这个提示很明显了吧,消除歧义加parentheses就好了:
  1. grep { (-s) < 1000 }
复制代码

论坛徽章:
0
发表于 2012-08-13 12:19 |显示全部楼层
回复 7# sjdy521
C:\windows>perl -e "print map{qq($_\n)} grep{-s lt 2000} glob('*.bmp')"
如改成{ (-s) < 2000}得到的结果不一样。

   

论坛徽章:
0
发表于 2012-08-14 13:40 |显示全部楼层
本帖最后由 slayercboy 于 2012-08-14 13:42 编辑

回复 8# mouse.rice


结果自然不一样,
lt比较的是ASCII码大小,
而'<'比较的是数值大小,
在位数不同的时候会出现差异
比如用lt比较9和100,结果是9大于100,
而用'<'结果是9小于100
你可以用如下代码测试:
  1. perl -e 'if (9 lt 100) {print "1\n";} else {print "2\n";}'
复制代码
结果为2
  1. perl -e 'if (9 < 100) {print "1\n";} else {print "2\n";}'
复制代码
结果为1

建议使用6L的方法

论坛徽章:
0
发表于 2012-08-14 16:52 |显示全部楼层
命令行运行perl,shell转义?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP