为什么我不能写@F[1..-1]来获取元素1..last?

发布于 2024-07-27 20:19:49 字数 468 浏览 9 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

爱要勇敢去追 2024-08-03 20:19:49

您的问题是 @a[b..c] 语法涉及两个不同的操作。 首先对 b..c 进行求值,返回一个列表,然后用该列表对 @a[] 进行求值。 范围运算符 .. 不知道它用于数组下标,因此就其而言,1 到 -1 之间没有任何内容。 它返回空列表,因此数组返回一个空切片。

Your problem is the @a[b..c] syntax involves two distinct operations. First b..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.

几度春秋 2024-08-03 20:19:49

在数组切片中 .. 没有什么特别的; 它只是生成请求的范围,然后查找该范围的切片。

所以 @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[()].

难以启齿的温柔 2024-08-03 20:19:49
print join ', ', 1..$#F; # 1, 2, 3, 4, 5, 6, ...
print join ', ', 1..-1;  #

其原因是 '..' 运算符在数组下标内部不执行任何特殊操作。

在列表上下文中,它返回从左值到右值计数(递增)的值列表。 如果左侧值大于右侧值,则返回空列表。


$#F 是最后一个元素的索引,等于长度减一“@F -1”。 (如果长度至少为 1。)

$F[-1] 只是一种特殊情况,为了更容易从另一端获取元素,而无需必须手动计算位置。

$F[-1] === $F[ @F -1 ] === $F[ $#F ]

@F[ 1 .. (@F -1) ] === @F[ 1 .. $#F ]

@F[ 1 .. (@F -2) ] === @F[ 1 .. ( $#F -1 ) ]

知道这一点,您可以在范围运算符中使用变量:

use strict;
use warnings;
use feature 'say';

sub list{
  my($arr,$first,$last) = @_;

  $first = @$arr + $first if $first < 0;
  $last  = @$arr + $last  if $last  < 0;

  return @$arr[ $first .. $last ];
}

my @F = 1..3;

say join ', ', list(\@F,1,-1)
2, 3

注意:这是一个不完整的示例,对于某些边缘情况它无法正常工作

print join ', ', 1..$#F; # 1, 2, 3, 4, 5, 6, ...
print join ', ', 1..-1;  #

The reason for this is that the '..' operator doesn't do anything special inside of an array subscript.

In list context, it returns a list of values counting (up by ones) from the left value to the right value. If the left value is greater than the right value then it returns the empty list.


$#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.

$F[-1] === $F[ @F -1 ] === $F[ $#F ]

@F[ 1 .. (@F -1) ] === @F[ 1 .. $#F ]

@F[ 1 .. (@F -2) ] === @F[ 1 .. ( $#F -1 ) ]

Knowing this you can use variables in a range operator:

use strict;
use warnings;
use feature 'say';

sub list{
  my($arr,$first,$last) = @_;

  $first = @$arr + $first if $first < 0;
  $last  = @$arr + $last  if $last  < 0;

  return @$arr[ $first .. $last ];
}

my @F = 1..3;

say join ', ', list(\@F,1,-1)
2, 3

Note: this is an incomplete example, it won't work correctly for some edge cases

时光倒影 2024-08-03 20:19:49

$#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

冷夜 2024-08-03 20:19:49

规则是:

  • 如果数组索引非负(或者等效地大于 $[),则从数组开头开始计数(
  • 如果为负)则从数组末尾开始计数(
  • 如果给定)一个范围,返回一个数组切片

现在,就像如果 a > 则集合 (a,b) 为空。 b,集合1 .. -1也是空的。 因此,

@a[ empty set ]

对应于一个空数组切片。

The rules are:

  • if the array index is non-negative (or, equivalently, greater than $[), start counting at the beginning of the array
  • if it is negative, start counting at the end
  • if given a range, return an array slice

Now, just like the set (a,b) is empty if a > b, the set 1 .. -1 is also empty. Therefore,

@a[ empty set ]

corresponds to an empty array slice.

記柔刀 2024-08-03 20:19:49

[] 运算符将接受单个索引或范围。

-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文