2011年10月21日 星期五

Arrow Notation 需知 $rarray->[0,2]等同於 $rarray->[2] 啦!





Caution: This notation works only for single indices, not for slices. Consider the following:
print $rarray->[0,2]; # Warning: This is NOT an indirect array slice.


@array = (1..10);
$rarray = \@array;
print $rarray->[0,2];#等同於print $rarray -> [2];


(切記: enumerated or comma-separated list 
總是 returns 最後一個元素在實數語境中 in a scalar context.)




Perl treats the stuff within the brackets as a comma-separated expression that yields the last term in the
array: 2. Hence, this expression is equivalent to $rarray->[2], which is an index lookup, not a slice.


(Recall the rule mentioned earlier: An enumerated or comma-separated list always returns the last
element in a scalar context.)

沒有留言:

張貼留言