Perl 中的“||”如何工作?

发布于 2024-09-24 22:29:36 字数 394 浏览 6 评论 0原文

|| 在 Perl 中如何工作?我想实现c风格的||操作。

@ARRAY=qw(one two THREE four);

$i=0;

if(($ARRAY[2] ne "three")||($ARRAY[2] ne "THREE"))         #What's the problem with this
{
   print ":::::$ARRAY[2]::::::\n";
}


while(($ARRAY[$i] ne "three")||($ARRAY[$i] ne "THREE"))       #This goes to infinite loop

{

 print "->$ARRAY[$i]\n";
   $i=$i+1;

}

How does the || works in Perl? I want to achieve c style || operation.

@ARRAY=qw(one two THREE four);

$i=0;

if(($ARRAY[2] ne "three")||($ARRAY[2] ne "THREE"))         #What's the problem with this
{
   print ":::::$ARRAY[2]::::::\n";
}


while(($ARRAY[$i] ne "three")||($ARRAY[$i] ne "THREE"))       #This goes to infinite loop

{

 print "->$ARRAY[$i]\n";
   $i=$i+1;

}

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

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

发布评论

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

评论(5

吻安 2024-10-01 22:29:36

它的工作原理与您想象的完全一样。然而,你对你的情况有一个想法。每个值要么不是一个值,要么不是另一个值。

我相信您可能想要

if ($ARRAY[2] ne 'three' && $ARRAY[2] ne 'THREE') { ...

或者

if ($ARRAY[2] eq 'three' || $ARRAY[2] eq 'THREE') { ...

您可能还想要某种不区分大小写的比较方式,例如

if (lc $ARRAY[2] ne 'three') { ...

或可能是不区分大小写的正则表达式匹配。

It works exactly the way you thought it would. However, you have a thinko in your condition. Every value is either not one value or not another value.

I believe you might have wanted

if ($ARRAY[2] ne 'three' && $ARRAY[2] ne 'THREE') { ...

or

if ($ARRAY[2] eq 'three' || $ARRAY[2] eq 'THREE') { ...

You might also want some case-insensitive way of comparing, like

if (lc $ARRAY[2] ne 'three') { ...

or possibly a case-insensitive regexp match.

韶华倾负 2024-10-01 22:29:36

两个一般要点。 (1) 您的 Perl 脚本应包含 use strictuse warnings。 (2) 在大多数情况下,您可以直接迭代数组,完全避免使用下标。一个例子:

use strict;
use warnings;

my @ARRAY = qw(one two THREE four);

for my $item (@ARRAY){
    last if lc $item eq 'three';
    print $item, "\n";
}

Two general points. (1) Your Perl scripts should include use strict and use warnings. (2) In most situations, you can iterate directly over an array, avoiding subscripts entirely. An example:

use strict;
use warnings;

my @ARRAY = qw(one two THREE four);

for my $item (@ARRAY){
    last if lc $item eq 'three';
    print $item, "\n";
}
我很OK 2024-10-01 22:29:36

($ARRAY[2] ne "三") || 中($ARRAY[2] ne "THREE") || 是一个逻辑 or,这意味着如果两个表达式中至少有一个则返回 true是真的。好吧,它会检查第一个,如果它是真的,它甚至不会检查第二个。在这种情况下,无论如何,整体都是 true,因为 $ARRAY[2] 不能等于两个字符串。

嗯,就像 CI 相信的那样。您想实现什么目标?

In ($ARRAY[2] ne "three") || ($ARRAY[2] ne "THREE") the || is a logical or, which means it returns true if at least one of the two expressions is true. Well, it checks the first one and if it is true, it even does not check the second one. And in this case the whole will be true anyway, since $ARRAY[2] cannot be equal to both strings.

Well, it is just like in C I believe. What would you like to achieve?

走野 2024-10-01 22:29:36

while(($ARRAY[$i] ne "third")||($ARRAY[$i] ne "THREE"))

表达式 $ARRAY[$i] ne "三” 始终评估为 true。因此你有一个无限循环。 || 运算符具有短路行为,因此永远不会计算第二个表达式。

while(($ARRAY[$i] ne "three")||($ARRAY[$i] ne "THREE")) :

the expression $ARRAY[$i] ne "three" always evaluates to true. Therefore you have an infinite loop. The || operator has short-circuit behavior so the second expression is never evaluated.

断肠人 2024-10-01 22:29:36
 ($ARRAY[$i] ne "three") || ($ARRAY[$i] ne "THREE")) 

在任何情况下都是如此。要么不是“三”,要么不是“三”。您需要 &&

 ($ARRAY[$i] ne "three") || ($ARRAY[$i] ne "THREE")) 

This is going to be true in every case. It's either going to be not "three" or not "THREE". You want &&.

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