24小时热门版块排行榜    

查看: 324  |  回复: 4
当前主题已经存档。

yalefield

金虫 (文坛精英)

老汉一枚

[交流] 【讨论】刀劈神仙 [转载]

前几天的case:
CODE:
12
23
34
45
56
67

想变成:
CODE:
1223
3445
5667

一般大家都会想到:
CODE:
perl -pe 'chomp if $. % 2' file

这个最直观易懂。但是Randal(Perl hacker,教育家)给出这个方法:
CODE:
perl -pe 'chomp; $_ .= <>' file

这个实现得更巧妙,且没有取余运算,效率会更高。
后来John W.(another Perl hacker)秀了一个更smart的方法:
CODE:
perl -lpe '$\=--$|?$,:$/' file

代码写到这样就是神仙了~~ 反正偶一时半会没看明白~~
我也读过大小骆驼呀、高级编程啥的也读过一堆呀,怎么就想不到呢?
达到这一级,除了经验外,更多的是靠冰雪玲珑心了~~
--对,一定要有一颗玲珑的慧心~~
看看“Just another Perl hacker”有多少种写法就知道了。
神仙的代码一般人是看不懂的,好在有懂的人出来点拨:
CODE:
--$|   toggles between 1 and 0. It is this variable's behavior when (--$| or $|--)
       $| is initially 0. (NOTE: this behavior doesn't happen with ++$| or $|++).
-l      chomps the line input and, for when --$| == 1, prints with $\ = $,
      ($, is by default the empty string). When --$| == 0, prints with $\ = $/
       ($/ is by default = "\n").
-p     applies 'print()' as the last statement in the implicit 'while( ... )'  loop
So. in effect, on the first line in, chomps the line. Then, since $|==1 in the first
iteration, prints with a line end equal to $, (by default, the empty string).
In the next line, $| will equal 0, so the print will end with $/ (default value of "\n")

谁要是在我的组里用这种代码,我一定用刀劈他!
http://bbs2.chinaunix.net/viewthread.php?tid=1622036

[ Last edited by jjdg on 2010-1-4 at 21:08 ]
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

nebulaly

木虫 (著名写手)

★ ★
小木虫(金币+0.5):给个红包,谢谢回帖交流
波不动(金币+1,VIP+0):感谢讨论交流!有时候例如微软面试也会出一些类似的题目,但答得“差”,有时候反而是微软要的。 12-5 23:01
这个叫杂技

如果是开发产品,最好的写法永远是第一种
2楼2009-12-05 22:48:19
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wangen994

荣誉版主 (著名写手)

将军

这个是真看不懂,自己知道的太少了
3楼2009-12-05 23:03:00
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

波不动

木虫 (正式写手)

Wave No Move


小木虫(金币+0.5):给个红包,谢谢回帖交流
引用回帖:
Originally posted by wangen994 at 2009-12-5 23:03:
这个是真看不懂,自己知道的太少了

我也一样,呵呵。
端好自己的碗,吃好自己的饭。
4楼2009-12-05 23:06:40
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

runmiao

铁杆木虫 (正式写手)


小木虫(金币+0.5):给个红包,谢谢回帖交流
这个只能秀一下,咱还是老老实实做软件蓝领,写的代码别人能立马看懂,不用太多注释。
5楼2009-12-12 10:49:27
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 yalefield 的主题更新
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见