代码之家  ›  专栏  ›  技术社区  ›  Bluz

Perl字符串if/或比较运算符

  •  0
  • Bluz  · 技术社区  · 5 年前

    # Get new_status
    print STDERR "Please enter status value (active/inactive): ";
    ReadMode(1);
    my $new_status = ReadLine(0);
    ReadMode(0);
    print STDERR "\n";
    
    if ( ($new_status ne "active") || ($new_status ne "inactive") )
    {
      die "Status must be active/inactive.";
    }
    

    将始终返回“Status must be active/inactive”(状态必须为活动/非活动)。无论我键入什么?(active、inactive或任何其他选项,甚至只需按enter键。)

    代码似乎有效:

    • 我用绳子 两种情况下的运算符“ne”
    • 我使用OR运算符||

    很明显,Perl运算符的某些方面我还没有完全掌握。

    谢谢您!

    0 回复  |  直到 5 年前
        1
  •  2
  •   UjinT34    5 年前

    $new_status 不能等于 active inactive 同时,条件总是正确的。你可能需要 && 而不是 ||

    # option 1
    if ( ($new_status ne "active") && ($new_status ne "inactive") )
    ...
    # option 2
    unless ( ($new_status eq "active") || ($new_status eq "inactive") )
    ...
    #option 3
    my %VALID_STATUS = (
        'active' => 1,
        'inactive' => 1,
    );
    if (!$VALID_STATUS{$new_status})
    ...
    
        2
  •  2
  •   ikegami    5 年前
    if ( ($new_status ne "active") || ($new_status ne "inactive") )
    {
      die "Status must be active/inactive.";
    }
    

    让我们来研究一下逻辑。

    1. 我随便打些东西 random 随机的 不是“活动”也不是“不活动”,所以 if if (true or true) -这是真的。
    2. active . 第一次检查是假的,第二次检查是的,所以你得到 if (false or true) -这是真的。
    3. I型 inactive if (true or false) -这是真的。

    没有什么能让你 陈述错误。

    你的问题,你不想加入你的两个条款 or and 相反。

    (从这个老程序员那里得到一个提示-使用 而不是 && ||

    更新: 总而言之,你的代码中有太多的负面信息,你会把自己弄糊涂。在你的回答中,你(沉默!)更改 如果 unless

    我会这样写:

    my $valid = $new_status eq 'active' || $new_status eq 'inactive';
    if (not $valid) {
       die "...";
    }
    

    use List::Util 'any';
    
    if (not any { $new_status eq $_ } qw[active inactive] ) {
      die "...";
    }
    
        3
  •  2
  •   ikegami    5 年前

    您只想在以下情况下显示错误消息 $new_status 不是 active 以及 如果 不是 inactive ,所以

    if ( $new_status ne "active" || $new_status ne "inactive" )
    

    应该是

    if ( $new_status ne "active" && $new_status ne "inactive" )
    

    De Morgan's laws .

    • !( A || B ) 相当于 !A && !B
    • !( A && B ) 相当于 !A || !B

    所以,

    • 有效输入如果 $new_status eq 'active' || $new_status eq 'inactive'
    • !( $new_status eq 'active' || $new_status eq 'inactive' )
    • 输入无效,如果 !( $new_status eq 'active' ) && !( $new_status eq 'inactive' )
    • 输入无效,如果 $new_status ne 'active' && $new_status ne 'inactive'

    您需要习惯于看到以下内容:

    if ( $new_status ne "active" && $new_status ne "inactive" ) {
       die("Status must be active/inactive.\n");
    }
    

    $new_status eq "active" || $new_status eq "inactive"    # Thing that should be true.
       or die("Status must be active/inactive.\n");         # Or what to do when it isn't.
    
        4
  •  -5
  •   Bluz    5 年前

    所以我找到了我问题的答案。

    问题在于格式。

    我补充道:

    print Dumper $new_status;
    

    在我的代码中,输出是:

    $VAR1 = 'active
    ';
    

    所以我加了一个:

    chomp $new_status;
    

    $VAR1 = 'active';
    

    # Get new_status
    print STDERR "Please enter status value (active/inactive): ";
    ReadMode(1);
    my $new_status = ReadLine(0);
    ReadMode(0);
    print STDERR "\n";
    
    chomp $new_status;
    
    unless ( ($new_status eq "active") || ($new_status eq "inactive") )
    {
      die "Status must be active/inactive.";
    }