新的“每个@array”的行为在标量环境中

发布于 2024-11-07 19:16:54 字数 1235 浏览 3 评论 0原文

Perl 5.14 为我们提供了扩展的each 函数,它可以对数组和哈希进行操作:

在列表上下文中调用时,返回一个 2 元素列表,其中包含哈希下一个元素的键和值,或数组下一个元素的索引和值,以便您可以对其进行迭代。在标量上下文中调用时,仅返回哈希中的键(而不是值)或数组中的索引。

使用列表上下文的示例有效:

perl -E 'say $^V'

v5.14.0

perl -E '@a = (1..10); while (my ($i, $elem) = each @a) {say "\$a[$i] = $elem"}'

$a[0] = 1
$a[1] = 2
$a[2] = 3
$a[3] = 4
$a[4] = 5
$a[5] = 6
$a[6] = 7
$a[7] = 8
$a[8] = 9
$a[9] = 10

但是在标量上下文中,我什么也得不到:

perl -E '@a = (1..10); while (my $i = each @a) {say $i}'

任何人都可以提供任何见解吗?我有一种感觉,当有人指出我的错误时,这将是一个沉重的打击,但也许不是。

编辑:实际上 while 循环与它无关:

perl -E '@a = (1..10); $i = each @array; say $i'

也没有给出任何输出。 s'@array'@a ' 哎呀。

编辑2:

根据 daxim 的评论:

perl -MDevel::Peek -E'@a = (1..10); Dump each @a'

SV = IV(0x161ce58) at 0x161ce68
  REFCNT = 1
  FLAGS = (TEMP,IOK,pIOK)
  IV = 0

但是我不知道这告诉了我什么。

编辑3:

看起来循环退出是因为第一个索引是0,或者是假。我已经提交了一个错误( http://rt.perl.org/rt3 /Ticket/Display.html?id=90888 ),因为这似乎不是所需的行为。

Perl 5.14 gives us the extended each function which operates on arrays as well as hashes:

When called in list context, returns a 2-element list consisting of the key and value for the next element of a hash, or the index and value for the next element of an array, so that you can iterate over it. When called in scalar context, returns only the key (not the value) in a hash, or the index in an array.

An example using list context works:

perl -E 'say $^V'

v5.14.0

perl -E '@a = (1..10); while (my ($i, $elem) = each @a) {say "\$a[$i] = $elem"}'

$a[0] = 1
$a[1] = 2
$a[2] = 3
$a[3] = 4
$a[4] = 5
$a[5] = 6
$a[6] = 7
$a[7] = 8
$a[8] = 9
$a[9] = 10

however in scalar context, I get nothing:

perl -E '@a = (1..10); while (my $i = each @a) {say $i}'

Can anyone offer any insight? I have a feeling that this will be a head slapper when someone points out my error, but perhaps not.

Edit: In fact the while loop has nothing to do with it:

perl -E '@a = (1..10); $i = each @array; say $i'

gives no output either. s'@array'@a' oops.

Edit 2:

As per daxim's comment:

perl -MDevel::Peek -E'@a = (1..10); Dump each @a'

SV = IV(0x161ce58) at 0x161ce68
  REFCNT = 1
  FLAGS = (TEMP,IOK,pIOK)
  IV = 0

however I have no idea what that tells me.

Edit 3:

It seems that the loop exits because the first index is 0, or false. I have filed a bug ( http://rt.perl.org/rt3/Ticket/Display.html?id=90888 ) since this doesn't seem to be the desired behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你怎么这么可爱啊 2024-11-14 19:16:54

您需要执行 while (define( my $i = every @array )) { say $i } 否则它将停止在第一个索引 (0) 处,因为它是 false。

You need to do while (defined( my $i = each @array )) { say $i } otherwise it'll stop at the first index (0) since it is false.

凉城 2024-11-14 19:16:54

【该帖子内容有误。我的测试肯定有错误。由于存在好的评论,我不会删除它。 ]

标量每个 %hash 的工作归功于每个哈希都有一个与其关联的迭代器。我怀疑这样的迭代器没有添加到数组中。

这是一个错误,正如 each 所说:

在标量上下文中调用时,返回仅哈希中的键(而非值)或数组中的索引。

请使用 perlbug 提交报告。

[ The contents of this post are wrong. There must have been a bug in my test. I'm not deleting it due to the presence of good comments. ]

scalar each %hash works thanks to each hash having an iterator associated with it. I suspect that such an iterator wasn't added to arrays.

This is a bug, as each says:

When called in scalar context, returns only the key (not the value) in a hash, or the index in an array.

Please file a report using perlbug.

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