茴字的多种写法 -- 论神仙代码

case:
12
23
34
45
56
67
想变成:
1223
3445
5667
一般大家都会想到:
perl -pe 'chomp if $. % 2' file
这个最直观易懂。

但是Randal(Perl hacker,教育家)给出这个方法:

perl -pe 'chomp; $_ .= <>' file
这个实现得更巧妙(偶已解释过),且没有取余运算,效率会更高。

后来John W.(another Perl hacker)秀了一个更smart的方法:

perl -lpe '$\=--$|?$,:$/' file
代码写到这样就是神仙了~~ 反正偶一时半会没看明白~~

我也读过大小骆驼呀、高级编程啥的也读过一堆呀,怎么就想不到呢?

达到这一级,除了经验外,更多的是靠冰雪玲珑心了~~

    --对,一定要有一颗玲珑的慧心~~

看看“Just another Perl hacker”有多少种写法就知道了。

神仙的代码一般人是看不懂的,好在有懂的人出来点拨:

--$|   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")

这就是Perl的魅力!聪者自聪,愚者自愚,Perl还是Perl~~

转载于:https://my.oschina.net/kiwivip/blog/112329

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值