Perl的:请给我解释而有下列行为() [英] Perl: Please explain to me the following behaviours of while()

查看:140
本文介绍了Perl的:请给我解释而有下列行为()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.为什么做而(小于$文件句柄>)通过无缝的文件重复,但不会通过列表或数组没有操纵数组本身迭代(就像使用而($ _ =转移@array){打印$ _} ?怎么会这样?

1.Why does while (<$filehandle>) iterate through a file seemlessly, but it doesn't iterate through lists or arrays without manipulating the array itself (like using while ($_ = shift @array){print $_}? How does this happen?

2.How难道是而($ VAR = @array){$打印VAR} 管理​​贯穿全阵列来迭代?不应该 $ VAR 刚刚获得指标的数量 @array 键,返回值(因此创建一个无限循环)?

2.How is it that while ($var = @array){print $var} manages to iterate through the whole array? Shouldn't $var just get the number of indices in @array and return that value (so creating an infinite loop)?

从我的理解而()读语句的条件只要它发现真的循环。我感到困惑,虽然是它的周围文件句柄,列表和等。

From my understanding while() reads the conditionality of a statement and loops as long as it finds it true. What I find confusing though is its behaviour around filehandles, lists and the such.

请帮助我了解更多关于而()因为我觉得这是运营商非常混乱的时候。

Please help me understand more about while() as I find this operator very confusing sometimes.

推荐答案

让我们通过查看开始,而(小于$文件句柄&GT;)。这实际上是Perl的速记本:

Let's start by looking at while(<$filehandle>). This is actually Perl shorthand for this:

while (defined($_ = <$filehandle>)) {
    print $_;
}

通过循环每次所以,你从文件读取的下一行,如果没有定义它,你会退出圈外。如果它被定义,你会继续。如您所愿,,而将循环,只要条件计算为true。

So each time through the loop, you're reading the next line from the file, and if it is not defined, you'll quit out of the loop. If it is defined, you'll continue. As you expect, while will loop as long as the condition evaluates to true.

您将能够看到,通过运行的perl -MO = Deparse program.pl 。您还可以检查出下回路控制的文档。钻石运算符是同样的事情子程序的readline (它从文件中读取下一行)。

You'll be able to see that by running perl -MO=Deparse program.pl. You can also check out the documentation under Loop Control. The diamond operator is the same thing as the subroutine readline (it reads the next line from the file).

在一段时间($ _ =转移@array){打印$ _} ,你会被修改阵列的情况下,当为<$ C $的条件C>,而循环检查。子程序转变删除数组的第一个元素,并返回它。因此, $ _ 将被设置正确,但你的阵列将开始失去的元素。

In the case for while ($_ = shift @array){print $_}, you will be modifying the array when the condition for the while loop is checked. The subroutine shift removes the first element of the array and returns it. So $_ will be set correctly, but your array will start losing elements.

在接下来的情况下,而($ VAR = @array){$打印VAR} $ VAR 将等于阵列中的元素的数量。这是因为你使用 @array 在标量上下文。这将主要可能是因为从 perlsyn

In the next case, while ($var = @array){print $var}, $var will be equal to the number of elements in the array. It's because you're using @array in a scalar context. This will mostly likely evaluate to true because from perlsyn

数字0,字符串0和,空列表()和民主基金
  在布尔上下文全是假的。所有其他值都为真。

The number 0, the strings '0' and "" , the empty list () , and undef are all false in a boolean context. All other values are true.

把这个单行例如:

perl -le 'my @array = qw(a b c d e); my $var = @array; print $var'

这将打印数 5 ,因为有数组中的5个元素。如果你有你的阵列中0元素,而循环将不会执行,它会停留在一个无限循环如果在这1个或多个元素。

It will print the number 5, because there are 5 elements in the array. The while loop will not execute if you have 0 elements in your array, and it will be stuck in an infinite loop if there are 1 or more elements in it.

例如:

### This code will print 5 forever ###
my @array = qw(hi this is a test);
my $var;
while ( $var =  @array ) {
    print "$var\n";
}

对于你的问题2,这听起来像你得到不同的结果,但这确实会卡在一个循环我。

For your question 2, it sounds like you are getting different results but this does get stuck in a loop for me.

这篇关于Perl的:请给我解释而有下列行为()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆