Perl If 和 Elsif 循环帮助
好吧,我还是 Perl 的新手,所以这个问题的答案可能看起来相当明显,但我在 Python 中做了一些工作,并且在学习 if、elsif 和 else 循环时遇到了问题;具体来说,它们无法正常工作。这是我的代码:
my $x = 0;
print "X has been set to ". $x .".\n";
while ($x<11)
{
$x++;
print "The value of x is now ". $x .".\n";
if ($x>4, $x<7){
print "Something\n";
system ("Pause");
}
elsif ($x>7, $x>11){ #<--Here
print "Something else\n";
system ("Pause");
}
elsif ($x==11){
print "Last line\n";
}
} #<-- and Here
system "Pause";
也许我的问题现在很明显,但如果不是,问题是它似乎没有评估任何表达式;它只是打印它找到的第一个循环,在本例中是 if 循环。如果我删除它,或者注释掉它,它会直接进入第一个 elsif 循环;也就是说,无论 x 的值是什么,它都会打印它找到的第一个循环,而不进行任何评估。当我添加时,
use strict;
use warnings;
我收到警告“在第 16 行的 void context 中无用使用数字 gt (>)”,第 24 行也是如此。我在原始代码中用箭头标记了这些警告还有这里。我是否在做某事/没有做我应该做的事?
Alright, I'm still a newbie in Perl, so the answer to this question may seem fairly obvious, but I've done some work in Python, and I've encountered a problem with learning the if, elsif, and else loops; specifically, that they don't work properly. Here's my code:
my $x = 0;
print "X has been set to ". $x .".\n";
while ($x<11)
{
$x++;
print "The value of x is now ". $x .".\n";
if ($x>4, $x<7){
print "Something\n";
system ("Pause");
}
elsif ($x>7, $x>11){ #<--Here
print "Something else\n";
system ("Pause");
}
elsif ($x==11){
print "Last line\n";
}
} #<-- and Here
system "Pause";
Maybe my problem is obvious by now, but if not, the problem is that it doesn't seem to be evaluating any of the expressions; it just prints the first loop it finds anyway, which in this case is the if loop. If i remove it, or comment it out, it goes straight to the first elsif loop; that is, no matter the value of x, it prints the first loop it finds without any sort of evaluation. When I added
use strict;
use warnings;
I got the warning "Useless use of numeric gt (>) in void context at line 16" and the same thing for line 24. I marked those out in the original code with the arrows and here. Am I doing something/not doing something I should be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
if (expr1, expr2)
将忽略expr1
。您可能指的是and
或&&
吗? (此外,如果是这样,您的elsif
应该测试x<11
。)if (expr1, expr2)
will ignoreexpr1
. Did you perhaps meanand
or&&
? (Also, if so, yourelsif
should be testingx<11
.)