Re: [問題] 點點(..)運算子兩問

作者: scwg ( )   2013-10-29 02:15:40
man perlop
The right operand is not evaluated while the operator is in the
"false" state, and the left operand is not evaluated while the
operator is in the "true" state. The precedence is a little lower
than || and &&. The value returned is either the empty string for
false, or a sequence number (beginning with 1) for true. The
sequence number is reset for each range encountered. The final
sequence number in a range has the string "E0" appended to it, which
doesn't affect its numeric value, but gives you something to search
for if you want to exclude the endpoint. You can exclude the
beginning point by waiting for the sequence number to be greater than
1.
所以 3..5 每次拿到的值是 '' '' 1 2 3E0 '' ''
3E0 的數值還是 3, 但是可以拿來判斷是不是最後一個
結論: perl 好噁心
※ 引述《AntaresStar ()》之銘言:
: 回到原本的問題 自問自答一下 XD
: 改成這樣
: $arr=[0,1,2,3,4,5,6,7];
: while(<DATA>) {
: print $arr->[3..5];
: }
: __DATA__
: A
: B
: C
: D
: E
: F
: G
: 會印出0012300
: 於是可以發現在->[]裡的..確實被當成flip-flop
: 而且還可以看到flip-flop為true的時候 實際上值會從1開始一直++
: ※ 引述《CindyLinz (Cindy Wang)》之銘言:
: : 就這樣的意思呀~
: : sub processLine {
: : if( 3 .. 5 ) {
: : print $_;
: : }
: : }
: : while(<>){
: : processLine();
: : }
: : processLine 裡面的 3 .. 5 不會隨著進出 processLine 而重置~
: : 用在這邊是方便啦, 只是這種隱藏地永久存在的狀態是比較危險的東西 :Q
作者: AntaresStar   0000-00-00 00:00:00
竟然真的有寫在說明書上 @@
作者: cutekid (可愛小孩子)   0000-00-00 00:00:00
推(Y),又長知識了
作者: CindyLinz (Cindy Wang)   0000-00-00 00:00:00
推(Y), 也長知識了
作者: scwg ( )   0000-00-00 00:00:00
長知識是在說現在才知道 perl 好噁心嗎?
作者: CindyLinz (Cindy Wang)   0000-00-00 00:00:00
什麼什麼, 哪邊有講那句嗎? ^_<
作者: herculus6502 (金麟豈是池中物)   2012-01-10 08:45:00
推(Y),perl豈止噁心,簡直就是邪惡
作者: xatier (一切重來就好了...)   2012-01-14 11:27:00
Perl 超棒的 ><

Links booklink

Contact Us: admin [ a t ] ucptt.com