为什么我不能写@F[1..-1]来获取元素1..last?
在 Perl 中,数组索引 -1
表示最后一个元素:
@F=(1,2,3);
print $F[-1]; # result: 3
您也可以使用 $#
表示法,这里是 $#F
@F=(1,2,3);
print $F[$#F]; # result: 3
:当我想指定范围中的最后一个元素时,为什么 -1
和 $#F
不给出相同的结果:
print @F[1..$#F]; # 23
print @F[1..-1]; # <empty>
数组 @F[1. .-1]
确实应该包含从元素 1
到最后一个元素的所有元素,不是吗?
In Perl, the array index -1
means the last element:
@F=(1,2,3);
print $F[-1]; # result: 3
You can also use the $#
notation instead, here $#F
:
@F=(1,2,3);
print $F[$#F]; # result: 3
So why don't -1
and $#F
give the same result when I want to specify the last element in a range:
print @F[1..$#F]; # 23
print @F[1..-1]; # <empty>
The array @F[1..-1]
should really contain all elements from element 1
to the last one, no?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的问题是
@a[b..c]
语法涉及两个不同的操作。 首先对b..c
进行求值,返回一个列表,然后用该列表对@a[]
进行求值。 范围运算符..
不知道它用于数组下标,因此就其而言,1 到 -1 之间没有任何内容。 它返回空列表,因此数组返回一个空切片。Your problem is the
@a[b..c]
syntax involves two distinct operations. Firstb..c
is evaluated, returning a list, and then@a[]
is evaluated with that list. The range operator..
doesn't know it's being used for array subscripts, so as far as it's concerned there's nothing between 1 and -1. It returns the empty list, so the array returns an empty slice.在数组切片中 .. 没有什么特别的; 它只是生成请求的范围,然后查找该范围的切片。
所以
@a[-3..-1]
=>@a[-3,-2,-1]
和@a[1..3]
=>@a[1,2,3]
,但@a[1..-1]
变为@a[()]
。There's nothing special about .. in an array slice; it just generates the requested range and then the slice of that range is looked up.
So
@a[-3..-1]
=>@a[-3,-2,-1]
, and@a[1..3]
=>@a[1,2,3]
, but@a[1..-1]
becomes@a[()]
.其原因是 '
..
' 运算符在数组下标内部不执行任何特殊操作。$#F
是最后一个元素的索引,等于长度减一“@F -1
”。 (如果长度至少为 1。)$F[-1]
只是一种特殊情况,为了更容易从另一端获取元素,而无需必须手动计算位置。知道这一点,您可以在范围运算符中使用变量:
注意:这是一个不完整的示例,对于某些边缘情况它无法正常工作
The reason for this is that the '
..
' operator doesn't do anything special inside of an array subscript.$#F
is the index of the last element, which is the same as the length minus one '@F -1
'. ( If the length is at least one. )$F[-1]
is just a special case, to make it easier to get at elements from the other end, without having to calculate the position manually.Knowing this you can use variables in a range operator:
Note: this is an incomplete example, it won't work correctly for some edge cases
$#F 是 F 的最后一个有效索引,而不是元素的数量。
@F = (1,2,3); # 所以 $#F 是 2
$#F is the last valid index of F, not the number of elements.
@F = (1,2,3); # so $#F is 2
规则是:
$[
),则从数组开头开始计数(现在,就像如果 a > 则集合 (a,b) 为空。 b,集合
1 .. -1
也是空的。 因此,对应于一个空数组切片。
The rules are:
$[
), start counting at the beginning of the arrayNow, just like the set (a,b) is empty if a > b, the set
1 .. -1
is also empty. Therefore,corresponds to an empty array slice.
[] 运算符将接受单个索引或范围。
-1 被接受作为索引,表示最后一个元素的索引。 正确的方法是$#F。
1..-1 的范围是空的,这就是它不返回任何内容的原因,因为范围评估与索引的评估是分开的。
The [] operator will accept either a single index or a range.
-1 is accepted as index to mean the index of last element. The proper way is $#F.
A range of 1..-1 is empty, and that is why it returns nothing, because the range evaluation is separate from the evaluation of the index.