Perl equivalent of (Postgre)SQL BETWEEN operator?

前端 未结 5 728
囚心锁ツ
囚心锁ツ 2021-01-22 07:17

Is there such a thing? The equivalent of a <= expr <= b, or in SQL parlance expr BETWEEN a AND b, where expr is evaluated only once

相关标签:
5条回答
  • 2021-01-22 07:55

    you could use Range operator + smart macthing:

    if($expr ~~ [$a..$b])
    
    0 讨论(0)
  • 2021-01-22 07:56

    I do not think they correspond exactly, but take a look at the Range Operators.

    0 讨论(0)
  • 2021-01-22 08:01

    In Perl6, the comparison operators are chainable.

    http://perlcabal.org/syn/S03.html#Chained_comparisons:

    Perl 6 supports the natural extension to the comparison operators, allowing multiple operands:

    if 1 < $a < 100 { say "Good, you picked a number *between* 1 and 100." }
    if 3 < $roll <= 6              { print "High roll" }
    if 1 <= $roll1 == $roll2 <= 6  { print "Doubles!" }
    

    In Perl 5, they are not.

    0 讨论(0)
  • 2021-01-22 08:02

    There are a variety of ways to do that in Perl.

    if( $a < $x and $x < $b ){ ... }
    ... if $a < $x and $x < $b;
    
    use 5.10.1;
    if( $x ~~ [$a..$b] ){ ... }
    
    given( $x ){
      when( [$a..$b] ){ ... }
    }
    
    use 5.11.0; # development branch
    given( $x ){
      ... when [$a..$b];
    }
    
    0 讨论(0)
  • 2021-01-22 08:09

    I think this is your only bet.

    $x = expr;
    
    if ($a < $x && $x < $b) {
      # stuff
    }
    
    0 讨论(0)
提交回复
热议问题