`||'如何在Perl中工作?

vrb*_*lgi -2 perl short-circuiting logical-operators

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

}
Run Code Online (Sandbox Code Playgroud)

raf*_*afl 13

它的工作方式与您的想法完全一致.但是,你有一个思考你的情况.每个值都不是一个值不是另一个值.

我相信你可能想要

if ($ARRAY[2] ne 'three' && $ARRAY[2] ne 'THREE') { ...
Run Code Online (Sandbox Code Playgroud)

要么

if ($ARRAY[2] eq 'three' || $ARRAY[2] eq 'THREE') { ...
Run Code Online (Sandbox Code Playgroud)

您可能还需要一些不区分大小写的比较方法,例如

if (lc $ARRAY[2] ne 'three') { ...
Run Code Online (Sandbox Code Playgroud)

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


FMc*_*FMc 5

两个一般要点.(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";
}
Run Code Online (Sandbox Code Playgroud)